3D Plots in PGFPlots
Create three-dimensional surface plots, mesh plots, and 3D line graphs.
Basic 3D Surface Plot
\begin{tikzpicture}
\begin{axis}[
view={60}{30},
xlabel={$x$},
ylabel={$y$},
zlabel={$z$}
]
\addplot3[
surf,
domain=-2:2,
domain y=-2:2,
samples=25
] {x^2 + y^2};
\end{axis}
\end{tikzpicture}Mesh Plot
\begin{tikzpicture}
\begin{axis}[
view={45}{45},
colormap/cool
]
\addplot3[
mesh,
domain=-2:2,
domain y=-2:2,
samples=20
] {sin(deg(x))*cos(deg(y))};
\end{axis}
\end{tikzpicture}3D Line Plot
\begin{tikzpicture}
\begin{axis}[
view={60}{30},
xlabel={$x$},
ylabel={$y$},
zlabel={$z$}
]
% Parametric 3D curve (helix)
\addplot3[
blue,
thick,
domain=0:6*pi,
samples=200,
samples y=0
] ({cos(deg(x))}, {sin(deg(x))}, {x/10});
\end{axis}
\end{tikzpicture}Colormaps
\begin{tikzpicture}
\begin{axis}[
view={60}{30},
colorbar,
colormap/viridis % or: hot, cool, jet, bluered, greenyellow
]
\addplot3[
surf,
shader=interp,
domain=-2:2,
domain y=-2:2,
samples=30
] {exp(-(x^2 + y^2))};
\end{axis}
\end{tikzpicture}Available colormaps: hot, cool, jet, viridis, bluered, greenyellow, blackwhite
Viewing Angle
\begin{tikzpicture}
\begin{axis}[
% view={azimuth}{elevation}
view={0}{90}, % Top view (2D projection)
title={Top View}
]
\addplot3[surf, domain=-2:2] {x^2 - y^2};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
view={45}{30}, % 3D perspective
title={3D View}
]
\addplot3[surf, domain=-2:2] {x^2 - y^2};
\end{axis}
\end{tikzpicture}Scatter Plot 3D
\begin{tikzpicture}
\begin{axis}[
view={60}{30},
xlabel={$x$},
ylabel={$y$},
zlabel={$z$}
]
\addplot3[
only marks,
mark=*,
mark size=2pt
] coordinates {
(0, 0, 0)
(1, 1, 1)
(2, 0, 2)
(1, 2, 3)
(0, 1, 1)
(2, 2, 4)
};
\end{axis}
\end{tikzpicture}Parametric Surface
\begin{tikzpicture}
\begin{axis}[
view={60}{30},
axis equal
]
% Sphere (parametric)
\addplot3[
surf,
domain=0:360,
domain y=0:180,
samples=30,
z buffer=sort
] (
{cos(x)*sin(y)},
{sin(x)*sin(y)},
{cos(y)}
);
\end{axis}
\end{tikzpicture}Multiple 3D Surfaces
\begin{tikzpicture}
\begin{axis}[
view={60}{30}
]
% First surface
\addplot3[
surf,
opacity=0.7,
domain=-2:2,
domain y=-2:2,
samples=20
] {x^2 + y^2};
% Second surface (plane)
\addplot3[
surf,
opacity=0.5,
domain=-2:2,
domain y=-2:2,
colormap/greenyellow
] {3};
\end{axis}
\end{tikzpicture}Shader Options
% Flat shading (one color per facet)
\addplot3[surf, shader=flat] {...};
% Interpolated shading (smooth gradients)
\addplot3[surf, shader=interp] {...};
% Faceted (mesh lines visible)
\addplot3[surf, shader=faceted] {...};
% Faceted with interpolated colors
\addplot3[surf, shader=faceted interp] {...};3D Bar Plot
\begin{tikzpicture}
\begin{axis}[
view={60}{30},
ybar3d,
xlabel={$x$},
ylabel={$y$},
zlabel={Count}
]
\addplot3[fill=blue!50] coordinates {
(0, 0, 5)
(0, 1, 8)
(1, 0, 3)
(1, 1, 10)
};
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Contour Plots - 2D projections of 3D data
- Colors and Styles - Customize colormaps
- Axis Customization - Advanced axis options
3D Plots in PGFPlots
Create three-dimensional surface plots, mesh plots, and 3D line graphs.
Basic 3D Surface Plot
\begin{tikzpicture}
\begin{axis}[
view={60}{30},
xlabel={$x$},
ylabel={$y$},
zlabel={$z$}
]
\addplot3[
surf,
domain=-2:2,
domain y=-2:2,
samples=25
] {x^2 + y^2};
\end{axis}
\end{tikzpicture}Mesh Plot
\begin{tikzpicture}
\begin{axis}[
view={45}{45},
colormap/cool
]
\addplot3[
mesh,
domain=-2:2,
domain y=-2:2,
samples=20
] {sin(deg(x))*cos(deg(y))};
\end{axis}
\end{tikzpicture}3D Line Plot
\begin{tikzpicture}
\begin{axis}[
view={60}{30},
xlabel={$x$},
ylabel={$y$},
zlabel={$z$}
]
% Parametric 3D curve (helix)
\addplot3[
blue,
thick,
domain=0:6*pi,
samples=200,
samples y=0
] ({cos(deg(x))}, {sin(deg(x))}, {x/10});
\end{axis}
\end{tikzpicture}Colormaps
\begin{tikzpicture}
\begin{axis}[
view={60}{30},
colorbar,
colormap/viridis % or: hot, cool, jet, bluered, greenyellow
]
\addplot3[
surf,
shader=interp,
domain=-2:2,
domain y=-2:2,
samples=30
] {exp(-(x^2 + y^2))};
\end{axis}
\end{tikzpicture}Available colormaps: hot, cool, jet, viridis, bluered, greenyellow, blackwhite
Viewing Angle
\begin{tikzpicture}
\begin{axis}[
% view={azimuth}{elevation}
view={0}{90}, % Top view (2D projection)
title={Top View}
]
\addplot3[surf, domain=-2:2] {x^2 - y^2};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
view={45}{30}, % 3D perspective
title={3D View}
]
\addplot3[surf, domain=-2:2] {x^2 - y^2};
\end{axis}
\end{tikzpicture}Scatter Plot 3D
\begin{tikzpicture}
\begin{axis}[
view={60}{30},
xlabel={$x$},
ylabel={$y$},
zlabel={$z$}
]
\addplot3[
only marks,
mark=*,
mark size=2pt
] coordinates {
(0, 0, 0)
(1, 1, 1)
(2, 0, 2)
(1, 2, 3)
(0, 1, 1)
(2, 2, 4)
};
\end{axis}
\end{tikzpicture}Parametric Surface
\begin{tikzpicture}
\begin{axis}[
view={60}{30},
axis equal
]
% Sphere (parametric)
\addplot3[
surf,
domain=0:360,
domain y=0:180,
samples=30,
z buffer=sort
] (
{cos(x)*sin(y)},
{sin(x)*sin(y)},
{cos(y)}
);
\end{axis}
\end{tikzpicture}Multiple 3D Surfaces
\begin{tikzpicture}
\begin{axis}[
view={60}{30}
]
% First surface
\addplot3[
surf,
opacity=0.7,
domain=-2:2,
domain y=-2:2,
samples=20
] {x^2 + y^2};
% Second surface (plane)
\addplot3[
surf,
opacity=0.5,
domain=-2:2,
domain y=-2:2,
colormap/greenyellow
] {3};
\end{axis}
\end{tikzpicture}Shader Options
% Flat shading (one color per facet)
\addplot3[surf, shader=flat] {...};
% Interpolated shading (smooth gradients)
\addplot3[surf, shader=interp] {...};
% Faceted (mesh lines visible)
\addplot3[surf, shader=faceted] {...};
% Faceted with interpolated colors
\addplot3[surf, shader=faceted interp] {...};3D Bar Plot
\begin{tikzpicture}
\begin{axis}[
view={60}{30},
ybar3d,
xlabel={$x$},
ylabel={$y$},
zlabel={Count}
]
\addplot3[fill=blue!50] coordinates {
(0, 0, 5)
(0, 1, 8)
(1, 0, 3)
(1, 1, 10)
};
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Contour Plots - 2D projections of 3D data
- Colors and Styles - Customize colormaps
- Axis Customization - Advanced axis options