Multiple Plots in PGFPlots
Create subplots, grouped figures, and complex multi-panel visualizations.
Side-by-Side Plots
\begin{tikzpicture}
\begin{axis}[
name=plot1,
width=5cm,
title={Plot A}
]
\addplot {x};
\end{axis}
\begin{axis}[
at={(plot1.east)},
anchor=west,
xshift=1cm,
width=5cm,
title={Plot B}
]
\addplot {x^2};
\end{axis}
\end{tikzpicture}Using GroupPlots Library
\usepgfplotslibrary{groupplots}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=2 by 2, % 2 columns, 2 rows
horizontal sep=1.5cm,
vertical sep=1.5cm
},
width=5cm,
height=4cm
]
\nextgroupplot[title={Plot 1}]
\addplot {x};
\nextgroupplot[title={Plot 2}]
\addplot {x^2};
\nextgroupplot[title={Plot 3}]
\addplot {sin(deg(x))};
\nextgroupplot[title={Plot 4}]
\addplot {cos(deg(x))};
\end{groupplot}
\end{tikzpicture}Shared Axes Labels
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=2 by 1,
xlabels at=edge bottom, % Only bottom x labels
ylabels at=edge left % Only left y labels
},
width=5cm,
height=5cm,
xlabel={$x$},
ylabel={$f(x)$}
]
\nextgroupplot
\addplot {x};
\nextgroupplot
\addplot {x^2};
\end{groupplot}
\end{tikzpicture}Stacked Plots
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=1 by 3,
vertical sep=0pt,
x descriptions at=edge bottom
},
width=10cm,
height=3cm,
xmin=0, xmax=10
]
\nextgroupplot[ylabel={Signal 1}]
\addplot[blue] {sin(deg(x))};
\nextgroupplot[ylabel={Signal 2}]
\addplot[red] {cos(deg(x))};
\nextgroupplot[ylabel={Signal 3}, xlabel={Time}]
\addplot[green!60!black] {sin(deg(x))*cos(deg(2*x))};
\end{groupplot}
\end{tikzpicture}Inset Plot
\begin{tikzpicture}
\begin{axis}[
name=main,
width=10cm,
height=7cm
]
\addplot[blue, thick, domain=0:10] {sin(deg(x))};
\end{axis}
% Inset plot
\begin{axis}[
at={(main.north east)},
anchor=north east,
xshift=-0.5cm,
yshift=-0.5cm,
width=4cm,
height=3cm,
xmin=0, xmax=2,
title={\footnotesize Zoomed},
title style={at={(0.5,0.9)}}
]
\addplot[blue, thick] {sin(deg(x))};
\end{axis}
\end{tikzpicture}Plots with Different Scales
\begin{tikzpicture}
% First axis (left y-axis)
\begin{axis}[
axis y line*=left,
xlabel={$x$},
ylabel={Temperature ($^\circ$C)},
ymin=0, ymax=100
]
\addplot[blue, thick] coordinates {
(0,20) (1,40) (2,60) (3,75) (4,80)
};
\label{plot:temp}
\end{axis}
% Second axis (right y-axis)
\begin{axis}[
axis y line*=right,
axis x line=none,
ylabel={Pressure (kPa)},
ymin=0, ymax=500
]
\addplot[red, thick, dashed] coordinates {
(0,100) (1,150) (2,250) (3,350) (4,400)
};
\label{plot:pressure}
\end{axis}
\end{tikzpicture}Minipage Approach
\begin{figure}
\centering
\begin{minipage}{0.45\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[width=\textwidth, title={Plot A}]
\addplot {x};
\end{axis}
\end{tikzpicture}
\subcaption{Linear function}
\end{minipage}
\hfill
\begin{minipage}{0.45\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[width=\textwidth, title={Plot B}]
\addplot {x^2};
\end{axis}
\end{tikzpicture}
\subcaption{Quadratic function}
\end{minipage}
\caption{Comparison of functions}
\end{figure}Common Group Plot Options
\begin{groupplot}[
group style={
group size=3 by 2, % columns by rows
horizontal sep=2cm, % horizontal spacing
vertical sep=2cm, % vertical spacing
xlabels at=edge bottom, % x labels position
ylabels at=edge left, % y labels position
xticklabels at=edge bottom, % x tick labels
yticklabels at=edge left, % y tick labels
group name=my plots % name for referencing
},
% Options applied to all plots
width=4cm,
height=3cm,
grid=major
]Next Steps
Continue learning PGFPlots:
- Data from Files - Import external data
- Axis Customization - Advanced axis options
- Legends - Shared legends
Multiple Plots in PGFPlots
Create subplots, grouped figures, and complex multi-panel visualizations.
Side-by-Side Plots
\begin{tikzpicture}
\begin{axis}[
name=plot1,
width=5cm,
title={Plot A}
]
\addplot {x};
\end{axis}
\begin{axis}[
at={(plot1.east)},
anchor=west,
xshift=1cm,
width=5cm,
title={Plot B}
]
\addplot {x^2};
\end{axis}
\end{tikzpicture}Using GroupPlots Library
\usepgfplotslibrary{groupplots}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=2 by 2, % 2 columns, 2 rows
horizontal sep=1.5cm,
vertical sep=1.5cm
},
width=5cm,
height=4cm
]
\nextgroupplot[title={Plot 1}]
\addplot {x};
\nextgroupplot[title={Plot 2}]
\addplot {x^2};
\nextgroupplot[title={Plot 3}]
\addplot {sin(deg(x))};
\nextgroupplot[title={Plot 4}]
\addplot {cos(deg(x))};
\end{groupplot}
\end{tikzpicture}Shared Axes Labels
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=2 by 1,
xlabels at=edge bottom, % Only bottom x labels
ylabels at=edge left % Only left y labels
},
width=5cm,
height=5cm,
xlabel={$x$},
ylabel={$f(x)$}
]
\nextgroupplot
\addplot {x};
\nextgroupplot
\addplot {x^2};
\end{groupplot}
\end{tikzpicture}Stacked Plots
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=1 by 3,
vertical sep=0pt,
x descriptions at=edge bottom
},
width=10cm,
height=3cm,
xmin=0, xmax=10
]
\nextgroupplot[ylabel={Signal 1}]
\addplot[blue] {sin(deg(x))};
\nextgroupplot[ylabel={Signal 2}]
\addplot[red] {cos(deg(x))};
\nextgroupplot[ylabel={Signal 3}, xlabel={Time}]
\addplot[green!60!black] {sin(deg(x))*cos(deg(2*x))};
\end{groupplot}
\end{tikzpicture}Inset Plot
\begin{tikzpicture}
\begin{axis}[
name=main,
width=10cm,
height=7cm
]
\addplot[blue, thick, domain=0:10] {sin(deg(x))};
\end{axis}
% Inset plot
\begin{axis}[
at={(main.north east)},
anchor=north east,
xshift=-0.5cm,
yshift=-0.5cm,
width=4cm,
height=3cm,
xmin=0, xmax=2,
title={\footnotesize Zoomed},
title style={at={(0.5,0.9)}}
]
\addplot[blue, thick] {sin(deg(x))};
\end{axis}
\end{tikzpicture}Plots with Different Scales
\begin{tikzpicture}
% First axis (left y-axis)
\begin{axis}[
axis y line*=left,
xlabel={$x$},
ylabel={Temperature ($^\circ$C)},
ymin=0, ymax=100
]
\addplot[blue, thick] coordinates {
(0,20) (1,40) (2,60) (3,75) (4,80)
};
\label{plot:temp}
\end{axis}
% Second axis (right y-axis)
\begin{axis}[
axis y line*=right,
axis x line=none,
ylabel={Pressure (kPa)},
ymin=0, ymax=500
]
\addplot[red, thick, dashed] coordinates {
(0,100) (1,150) (2,250) (3,350) (4,400)
};
\label{plot:pressure}
\end{axis}
\end{tikzpicture}Minipage Approach
\begin{figure}
\centering
\begin{minipage}{0.45\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[width=\textwidth, title={Plot A}]
\addplot {x};
\end{axis}
\end{tikzpicture}
\subcaption{Linear function}
\end{minipage}
\hfill
\begin{minipage}{0.45\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[width=\textwidth, title={Plot B}]
\addplot {x^2};
\end{axis}
\end{tikzpicture}
\subcaption{Quadratic function}
\end{minipage}
\caption{Comparison of functions}
\end{figure}Common Group Plot Options
\begin{groupplot}[
group style={
group size=3 by 2, % columns by rows
horizontal sep=2cm, % horizontal spacing
vertical sep=2cm, % vertical spacing
xlabels at=edge bottom, % x labels position
ylabels at=edge left, % y labels position
xticklabels at=edge bottom, % x tick labels
yticklabels at=edge left, % y tick labels
group name=my plots % name for referencing
},
% Options applied to all plots
width=4cm,
height=3cm,
grid=major
]Next Steps
Continue learning PGFPlots:
- Data from Files - Import external data
- Axis Customization - Advanced axis options
- Legends - Shared legends