Tx · LaTeX & TikZ

How to Choose and Change a 3D View in TikZ

Published By the 9to5Science editorial team

At a glance

  1. 1. Set the axes

    Choose the 2D direction and scale that TikZ should use for each of the x, y, and z directions.

  2. 2. Draw in 3D coordinates

    Use coordinates such as (1,2,3); TikZ projects them onto the page using the axis directions you set.

  3. 3. Change the view

    Change those directions to get a different oblique view, or use pgfplots and its view option for a 3D plot.

TikZ can draw points with three coordinates, but the page is two-dimensional. For an ordinary tikzpicture, you choose how each 3D axis is projected onto the page by setting the x, y, and z direction vectors. Changing those vectors changes the apparent viewing angle.

This is a parallel projection (often used for oblique drawings), not a camera that calculates perspective or decides which surfaces are hidden. You control the drawing and which edges or faces to show. If you are making a plotted 3D surface or data graph, pgfplots has a separate view option for setting azimuth and elevation.

Set the projected axis directions

In TikZ, each coordinate (a,b,c) is mapped to the page using the three axis vectors. For example, if the vectors for x, y, and z are x\mathbf{x}, y\mathbf{y}, and z\mathbf{z}, then (a,b,c) is drawn at the page position ax+by+cza\mathbf{x}+b\mathbf{y}+c\mathbf{z}.

Set the vectors as options on the tikzpicture environment:

\begin{tikzpicture}[
  x={(1cm,0cm)},
  y={(-0.6cm,-0.4cm)},
  z={(0cm,1cm)}
]
  % Draw with three-coordinate points here
\end{tikzpicture}

Here, positive x goes right, positive y goes down and left, and positive z goes up. The lengths of the vectors also set the scale of each direction. You can make an axis shorter or longer to change the proportions of the drawing.

Worked example: draw a cube

This complete example draws a cube using three-coordinate points. The same coordinates will look different if you change the axis vectors.

\documentclass[tikz,border=5pt]{standalone}
\begin{document}
\begin{tikzpicture}[
  x={(1cm,0cm)},
  y={(-0.6cm,-0.4cm)},
  z={(0cm,1cm)}
]
  \draw (0,0,0) -- (1,0,0) -- (1,1,0) -- (0,1,0) -- cycle;
  \draw (0,0,1) -- (1,0,1) -- (1,1,1) -- (0,1,1) -- cycle;
  \draw (0,0,0) -- (0,0,1);
  \draw (1,0,0) -- (1,0,1);
  \draw (1,1,0) -- (1,1,1);
  \draw (0,1,0) -- (0,1,1);
\end{tikzpicture}
\end{document}

For instance, the point (1,1,0) is drawn at the sum of one x vector and one y vector. The cube’s sides are therefore projected as slanted edges rather than all appearing at right angles. The example uses parallel projection: edges in the same 3D direction remain parallel on the page.

Change the view

Change the vectors and recompile. For example, to make the y direction point up and right instead of down and left, replace

y={(-0.6cm,-0.4cm)}

with

y={(0.6cm,0.4cm)}

To make an axis appear less steep, change the ratio between its horizontal and vertical components. To make it appear shorter, reduce both components. These are choices of projection, so there is no single correct vector set for every drawing. Try small changes and check that the resulting projected axes communicate the shape clearly.

For 3D plots in pgfplots

If you are plotting a surface or 3D data with pgfplots, use its view key instead of manually setting the TikZ basis vectors. It takes an azimuth angle and an elevation angle, in degrees:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[view={45}{30}]
    \addplot3[surf, domain=-1:1, domain y=-1:1]
      {x^2-y^2};
  \end{axis}
\end{tikzpicture}
\end{document}

Change view={45}{30} to another pair of angles to rotate the plotted view. The first value changes the azimuth around the vertical axis; the second changes the elevation. This controls the plot’s viewpoint, unlike the manual axis-vector method used for basic TikZ drawings.

Common mistakes

  • Using two coordinates for a 3D point. A point such as (1,2,3) has three components. A point like (1,2) omits z and lies in the z=0 plane, which is fine when that is intended.
  • Expecting TikZ to hide back edges automatically. Basic TikZ draws the paths you specify; it does not infer a solid’s geometry or determine which edges are occluded. Omit hidden edges or use a suitable drawing order and styles.
  • Changing only one vector without checking the result. The projected directions work together. If two become nearly parallel, the drawing can look flattened or ambiguous.
  • Using view in the wrong context. view={azimuth}{elevation} is a pgfplots axis option. For an ordinary TikZ picture, set the projected x, y, and z vectors instead.

References

  1. PGF/TikZ Manual
  2. PGFPlots Package

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.