How to Plot Different Scales on the Same PGFPlots Figure
At a glance
- 1. Choose the axis type
Use one axis for shared units; use two y-axes when the series have different units or ranges.
- 2. Set up the first axis
Choose its limits and labels, then plot the first data series.
- 3. Overlay the second axis
Match the plot dimensions and position, keep the same x-range, and set the second y-range independently.
- 4. Check readability
Use distinct colors and explain both scales; consider whether two axes could mislead readers.
When two data series have very different ranges—or represent different units—a single y-axis can make one series hard to read. In PGFPlots, the usual solution is to overlay two axis environments: one supplies the left y-axis and the other supplies the right y-axis.
The example below plots temperature on the left scale and relative humidity on the right. Both use the same x-values, but each has its own y-limits.
Overlay two axes
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
scale only axis,
width=10cm,
height=6cm,
at={(0,0)},
anchor=south west,
xmin=0, xmax=4,
ymin=0, ymax=100,
axis x line*=bottom,
axis y line*=left,
xlabel={Time},
ylabel={Temperature ($^\circ$C)},
ylabel style={color=blue},
yticklabel style={color=blue},
]
\addplot[blue, thick] coordinates {
(0,15) (1,30) (2,45) (3,65) (4,80)
};
\end{axis}
\begin{axis}[
scale only axis,
width=10cm,
height=6cm,
at={(0,0)},
anchor=south west,
xmin=0, xmax=4,
ymin=0, ymax=1,
axis x line=none,
axis y line*=right,
ylabel={Relative humidity (fraction)},
ylabel style={color=red},
yticklabel style={color=red},
]
\addplot[red, thick, dashed] coordinates {
(0,0.8) (1,0.7) (2,0.6) (3,0.5) (4,0.4)
};
\end{axis}
\end{tikzpicture}
\end{document}
This complete example needs the pgfplots package. The compat=1.18 setting selects a PGFPlots compatibility level; if your installed version is older, use a compatibility level it supports.
Why the axes line up
The two axis environments are placed at the same point with the same anchor, width, and height. The scale only axis option makes those dimensions refer to the plotting area rather than including labels and tick labels. This helps keep the data regions aligned even though the labels extend in different directions.
Both axes also have identical xmin and xmax values and use the same x-axis scale or transformation. Together, these conditions make a given horizontal position represent the same time in both series. The y-ranges differ: the left axis runs from 0 to 100, while the right runs from 0 to 1. Each plot is therefore scaled against its own y-axis.
The first axis draws the bottom x-axis and its label. The second uses axis x line=none so it does not draw a second, overlapping x-axis. The axis y line*=left and axis y line*=right options place the vertical axes on opposite sides. Matching the colors of each curve, y-axis label, and tick labels makes the association clear.
When one axis is enough
If the series use the same units and should be compared by their actual values, use a single axis environment and add both plots to it. PGFPlots will scale them using the same y-axis. Use separate axes only when you need genuinely different scales, such as temperature and relative humidity, or when one series would otherwise be unreadable because its range is much smaller.
A second y-axis can make visual comparisons misleading: changing either axis limits can change how similar the two curves appear. Label both axes with units, state which series uses which axis, and avoid implying that similar slopes or intersections have a direct numerical meaning unless that interpretation is justified.
Common mistakes
- Different x-limits or x-scales: If the axes use different horizontal limits or different x-axis transformations, points at the same screen position may represent different x-values. Keep
xminandxmaxthe same and use the same x-axis scale when the axes share an x-variable. - Different plot dimensions or positions: A small mismatch in
width,height,at, oranchorcan make the curves and axes misalign. - Drawing both x-axes: Leave the x-axis enabled on one environment and disable it on the other to avoid doubled ticks and labels.
- Forgetting the second scale: Each axis has its own limits. Set
yminandymaxon both environments rather than assuming one axis inherits the other’s range. - Using two scales just to fit the page: If the issue is that the graph is too large, change its dimensions or limits instead of adding an unnecessary second y-axis.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.