How Do You Determine a Line’s Slope in PGFPlots?
At a glance
- 1. Choose the slope
For a straight line in data coordinates, calculate rise divided by run using two points on the line.
- 2. Use data coordinates
In PGFPlots, axis cs places annotation points at their x and y data values, even when the axis limits are automatic.
- 3. Separate slope from angle
Automatic limits do not change the data slope, but the axis dimensions and ranges can change how steep the line looks.
- 4. Match axis units if needed
Use equal physical lengths for x and y data units when you want the drawn angle to represent the numerical slope.
A line’s mathematical slope is determined by its data coordinates, not by how PGFPlots scales the axes. If the line passes through and , its slope is , provided .
Automatic axis limits can make the same line look steeper or flatter in the finished figure. The key is to distinguish the slope in data units from the line’s visual angle on the page.
Calculate slope from the data
For a nonvertical straight line, choose any two distinct points on it and compute
For example, the line has slope . Between and , its y-value rises from to , so the rise is and the run is .
In PGFPlots, the expression inside \addplot describes the data relationship. Automatic axis limits may select the visible range, but they do not recalculate that relationship or change its slope.
Show the rise and run in a plot
Use axis cs:x,y to specify points in the plot’s data coordinates. The following complete example plots a line and marks a rise-and-run triangle. It requires the pgfplots package.
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
xlabel={$x$},
ylabel={$y$},
grid=both
]
\addplot[blue, thick, domain=-2:2, samples=2] {2*x+1};
% From (0,1) to (1,1) is run 1; from (1,1) to (1,3) is rise 2.
\draw[dashed, red] (axis cs:0,1) -- (axis cs:1,1) -- (axis cs:1,3);
\node[below] at (axis cs:0.5,1) {run $=1$};
\node[right] at (axis cs:1,2) {rise $=2$};
\end{axis}
\end{tikzpicture}
\end{document}
The axis limits are left to PGFPlots, while the annotation remains attached to the data points. The horizontal segment has length in x-data units and the vertical segment has length in y-data units, so their ratio gives the slope .
Why the line can look different
The angle you see depends on how many physical units on the page represent one x-unit and one y-unit. For linear axes, let the plot box have physical width and height , and let the displayed data ranges be and . A line with data slope has visual rise-to-run ratio
For instance, suppose , the x-range is , the y-range is , and the plotting area is cm wide by cm high. Its visual slope is . The line still has data slope ; it only appears less steep because the axes use different physical scales.
When the visual angle should represent the numerical slope, make one x-unit and one y-unit the same physical length. In PGFPlots, you can set equal unit-vector lengths, for example with x=1cm,y=1cm in the axis options. This can make the plot box large if the data ranges are large. The axis equal and axis equal image options are also useful when you want equal scaling, but check their effects on plot limits and layout for your figure.
Common mistakes
- Measuring the angle on the page: The angle is not the data slope unless the x- and y-unit lengths match.
- Using pixel or page distances as data values: Calculate rise and run from the coordinates, or use a triangle drawn with
axis cspoints. - Assuming automatic limits imply equal scales: Automatic limits choose a visible data range; they do not guarantee equal physical lengths per unit.
- Using a vertical line in the rise-over-run formula: If , the line is vertical and its slope is undefined.
For logarithmic axes, the displayed line’s angle does not correspond to a constant ordinary data slope in the same way. Compute slope in the original data coordinates when that is what you need; use the plotted angle only to describe the rendered figure.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.