Tx · LaTeX & TikZ

How Do You Determine a Line’s Slope in PGFPlots?

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose the slope

    For a straight line in data coordinates, calculate rise divided by run using two points on the line.

  2. 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. 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. 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 (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2), its slope is (y2−y1)/(x2−x1)(y_2-y_1)/(x_2-x_1), provided x2≠x1x_2\ne x_1.

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

m=y2−y1x2−x1.m=\frac{y_2-y_1}{x_2-x_1}.

For example, the line y=2x+1y=2x+1 has slope 22. Between x=0x=0 and x=1x=1, its y-value rises from 11 to 33, so the rise is 22 and the run is 11.

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 11 in x-data units and the vertical segment has length 22 in y-data units, so their ratio gives the slope 22.

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 WW and height HH, and let the displayed data ranges be Δx\Delta x and Δy\Delta y. A line with data slope mm has visual rise-to-run ratio

visual slope=m H ΔxW Δy.\text{visual slope}=m\,\frac{H\,\Delta x}{W\,\Delta y}.

For instance, suppose m=2m=2, the x-range is 44, the y-range is 88, and the plotting area is 88 cm wide by 66 cm high. Its visual slope is 2(6⋅4)/(8⋅8)=0.752(6\cdot4)/(8\cdot8)=0.75. The line still has data slope 22; 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 cs points.
  • 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 x2=x1x_2=x_1, 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

  1. The PGFPlots Package: Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.