TikZ: How to draw an isometric drawing in tikz
Like Andrew wrote, 3d is not in the documentation. I updated my answer because I introduced some mistakes and complications. First we need to define the vectors for the xyz system, then with the 3d
library options, we can work in a specific plane.
canvas is xy plane at z=0
I design the plane xy with z=0
, I made a mistake with yx
because in this case I exchange the vectors x
and y
.
Here a list of the options in 3d
coordinate system xyz cylindrical
coordinate system xyz spherical
/tikz/cs/longitude/
/tikz/cs/latitude/
plane origin
plane x
plane y
canvas is plane
canvas is xy plane at z
canvas is yx plane at z
canvas is xz plane at y
canvas is zx plane at y
canvas is yz plane at x
canvas is zy plane at x
The code :
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{arrows,3d}
% see the explanation below
\makeatletter
\tikzoption{canvas is xy plane at z}[]{%
\def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}%
\def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}%
\def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}%
\tikz@canvas@is@plane
}
\makeatother
\begin{document}
\begin{tikzpicture}
[x={(0.866cm,0.5cm)}, y={(-0.866cm,0.5cm)}, z={(0cm,1cm)}, scale=2]
\draw[->,red] (0,0,0) -- (1,0,0);
\draw[->,red] (0,0,0) -- (0,1,0);
\draw[->,red] (0,0,0) -- (0,0,1);
\begin{scope}[canvas is xy plane at z=0]
\draw[blue,shift={(1.5,0)}] (0,0) -- (1,0)--(1,1)--(0,1)--cycle;
\draw[blue,shift={(3,0)}] (0,0) -- (1,0)--(1,1)--(0,1)--cycle;
\end{scope}
\end{tikzpicture}
\end{document}
The code for the option used in my example is
\tikzoption{canvas is xy plane at z}{%
\tikz@addtransform{\pgftransformshift{\pgfpointxyz{0}{0}{#1}}}%
}
This is only a shift transformation.
Update
As Jake noticed in this answer grid in 3d
The implementation of canvas is xy plane at z in tikzlibrary3d.code.tex is incorrect, it merely sets a coordinate shift, but doesn't activate the full transformation code necessary. You can redefine the key correctly within your document:
\makeatletter
\tikzoption{canvas is xy plane at z}[]{%
\def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}%
\def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}%
\def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}%
\tikz@canvas@is@plane
}
\makeatother
I forgot this and it is why I use yx
instead of xy
in my first attempt.
As @Jake pointed out in a comment, you can specify the coordinate system of your choice as an option of the tikzpicture
environment. Here is an example:
\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[y={(-1cm,0.5cm)},x={(1cm,0.5cm)}, z={(0cm,1cm)}]
% coordinate system
\coordinate (O) at (0, 0, 0);
\draw[-latex] (O) -- +(1, 0, 0) node [right] {$x$};
\draw[-latex] (O) -- +(0, 1, 0) node [left] {$y$};
\draw[-latex] (O) -- +(0, 0, 1) node [above] {$z$};
% rectangles
\draw (3,-1.5,0) -- (3,1.5,0) -- (5,1.5,0) -- (5,-1.5,0) -- cycle;
\draw (6,-1.5,0) -- (6,1.5,0) -- (8,1.5,0) -- (8,-1.5,0) -- cycle;
\end{tikzpicture}
\end{document}
Instead of giving the base vectors as a (x,y) tuple
[y={(-1cm,0.5cm)},x={(1cm,0.5cm)}, z={(0cm,1cm)}]
,
I prefer to use (angle:length)
\begin{tikzpicture}[x={(90:1cm)}, y={(0:1cm)}, z={(45:0.7cm)}]
This gives you a better understanding of the vector directions.