Contour Plots in PGFPlots
Create contour maps showing level curves of 3D surfaces.
Basic Contour Plot
\begin{tikzpicture}
\begin{axis}[
view={0}{90}, % Top-down view
xlabel={$x$},
ylabel={$y$}
]
\addplot3[
contour gnuplot={
levels={0.5, 1, 1.5, 2, 2.5}
},
domain=-2:2,
domain y=-2:2,
samples=30
] {x^2 + y^2};
\end{axis}
\end{tikzpicture}Filled Contour Plot
\begin{tikzpicture}
\begin{axis}[
view={0}{90},
colorbar,
colormap/viridis
]
\addplot3[
contour filled={
number=10
},
domain=-2:2,
domain y=-2:2,
samples=50
] {sin(deg(x))*cos(deg(y))};
\end{axis}
\end{tikzpicture}Contour with Labels
\begin{tikzpicture}
\begin{axis}[
view={0}{90},
xlabel={$x$},
ylabel={$y$}
]
\addplot3[
contour gnuplot={
levels={1, 2, 3, 4},
labels=true,
label style={font=\footnotesize}
},
domain=-2:2,
domain y=-2:2,
samples=40
] {x^2 + y^2};
\end{axis}
\end{tikzpicture}Contour on 3D Surface
\begin{tikzpicture}
\begin{axis}[
view={60}{30}
]
% Surface
\addplot3[
surf,
opacity=0.7,
domain=-2:2,
domain y=-2:2,
samples=25
] {exp(-(x^2 + y^2))};
% Contour lines on surface
\addplot3[
contour gnuplot={
draw color=black,
levels={0.2, 0.4, 0.6, 0.8}
},
domain=-2:2,
domain y=-2:2,
samples=30
] {exp(-(x^2 + y^2))};
\end{axis}
\end{tikzpicture}Contour Projected to Base
\begin{tikzpicture}
\begin{axis}[
view={60}{30}
]
% Surface
\addplot3[
surf,
domain=-2:2,
domain y=-2:2,
samples=25
] {x^2 - y^2};
% Contour at z=0 (base)
\addplot3[
contour gnuplot={
draw color=red,
contour dir=z,
contour label={font=\tiny}
},
domain=-2:2,
domain y=-2:2,
samples=30,
z filter/.code={\def\pgfmathresult{-4}} % Project to z=-4
] {x^2 - y^2};
\end{axis}
\end{tikzpicture}Custom Contour Levels
\begin{tikzpicture}
\begin{axis}[
view={0}{90},
colorbar
]
\addplot3[
contour gnuplot={
% Specific levels
levels={-0.8, -0.4, 0, 0.4, 0.8},
% Or use number of levels
% number=15,
draw color=black
},
thick,
domain=-pi:pi,
domain y=-pi:pi,
samples=50
] {sin(deg(x))*sin(deg(y))};
\end{axis}
\end{tikzpicture}Heat Map Style
\begin{tikzpicture}
\begin{axis}[
view={0}{90},
colorbar,
colormap/hot,
xlabel={$x$},
ylabel={$y$},
title={Temperature Distribution}
]
\addplot3[
surf,
shader=interp,
domain=-2:2,
domain y=-2:2,
samples=50
] {exp(-(x^2 + y^2)/2)};
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Polar Plots - Circular coordinate plots
- 3D Plots - Surface and mesh plots
- Colors and Styles - Custom colormaps
Contour Plots in PGFPlots
Create contour maps showing level curves of 3D surfaces.
Basic Contour Plot
\begin{tikzpicture}
\begin{axis}[
view={0}{90}, % Top-down view
xlabel={$x$},
ylabel={$y$}
]
\addplot3[
contour gnuplot={
levels={0.5, 1, 1.5, 2, 2.5}
},
domain=-2:2,
domain y=-2:2,
samples=30
] {x^2 + y^2};
\end{axis}
\end{tikzpicture}Filled Contour Plot
\begin{tikzpicture}
\begin{axis}[
view={0}{90},
colorbar,
colormap/viridis
]
\addplot3[
contour filled={
number=10
},
domain=-2:2,
domain y=-2:2,
samples=50
] {sin(deg(x))*cos(deg(y))};
\end{axis}
\end{tikzpicture}Contour with Labels
\begin{tikzpicture}
\begin{axis}[
view={0}{90},
xlabel={$x$},
ylabel={$y$}
]
\addplot3[
contour gnuplot={
levels={1, 2, 3, 4},
labels=true,
label style={font=\footnotesize}
},
domain=-2:2,
domain y=-2:2,
samples=40
] {x^2 + y^2};
\end{axis}
\end{tikzpicture}Contour on 3D Surface
\begin{tikzpicture}
\begin{axis}[
view={60}{30}
]
% Surface
\addplot3[
surf,
opacity=0.7,
domain=-2:2,
domain y=-2:2,
samples=25
] {exp(-(x^2 + y^2))};
% Contour lines on surface
\addplot3[
contour gnuplot={
draw color=black,
levels={0.2, 0.4, 0.6, 0.8}
},
domain=-2:2,
domain y=-2:2,
samples=30
] {exp(-(x^2 + y^2))};
\end{axis}
\end{tikzpicture}Contour Projected to Base
\begin{tikzpicture}
\begin{axis}[
view={60}{30}
]
% Surface
\addplot3[
surf,
domain=-2:2,
domain y=-2:2,
samples=25
] {x^2 - y^2};
% Contour at z=0 (base)
\addplot3[
contour gnuplot={
draw color=red,
contour dir=z,
contour label={font=\tiny}
},
domain=-2:2,
domain y=-2:2,
samples=30,
z filter/.code={\def\pgfmathresult{-4}} % Project to z=-4
] {x^2 - y^2};
\end{axis}
\end{tikzpicture}Custom Contour Levels
\begin{tikzpicture}
\begin{axis}[
view={0}{90},
colorbar
]
\addplot3[
contour gnuplot={
% Specific levels
levels={-0.8, -0.4, 0, 0.4, 0.8},
% Or use number of levels
% number=15,
draw color=black
},
thick,
domain=-pi:pi,
domain y=-pi:pi,
samples=50
] {sin(deg(x))*sin(deg(y))};
\end{axis}
\end{tikzpicture}Heat Map Style
\begin{tikzpicture}
\begin{axis}[
view={0}{90},
colorbar,
colormap/hot,
xlabel={$x$},
ylabel={$y$},
title={Temperature Distribution}
]
\addplot3[
surf,
shader=interp,
domain=-2:2,
domain y=-2:2,
samples=50
] {exp(-(x^2 + y^2)/2)};
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Polar Plots - Circular coordinate plots
- 3D Plots - Surface and mesh plots
- Colors and Styles - Custom colormaps