Tx · LaTeX & TikZ

How to Annotate a Slope Triangle in a PGFPlots Axis

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose two points

    Select two points on the plotted line and calculate their horizontal and vertical differences.

  2. 2. Draw the triangle

    Use axis cs coordinates to connect the points with horizontal and vertical segments.

  3. 3. Add labels

    Place nodes at data-coordinate positions to label the run, rise, and slope.

  4. 4. Check the scales

    Remember that the triangle shows data-coordinate changes, not necessarily the line’s visual angle.

To show a slope on a PGFPlots graph, choose two points on the plotted line and draw a right triangle between them. Its horizontal leg represents the change in xx (the run), and its vertical leg represents the change in yy (the rise). Label those legs and, if useful, write the slope as rise divided by run.

Use PGFPlots’ axis cs coordinate system for the triangle’s corners. These coordinates are expressed in the same data units as the axes, so the triangle stays aligned with the plotted data.

Complete example

The example plots y=0.5x+1y=0.5x+1 and marks the points (1,1.5)(1,1.5) and (5,3.5)(5,3.5). The run is 5−1=45-1=4, the rise is 3.5−1.5=23.5-1.5=2, and the slope is 2/4=1/22/4=1/2.

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    xmin=0, xmax=6,
    ymin=0, ymax=5,
    xlabel={$x$},
    ylabel={$y$},
    grid=both,
    width=10cm,
    height=7cm,
]
    % Plot the line.
    \addplot[blue, thick, domain=0:6, samples=2] {0.5*x + 1};

    % Mark the two points used to calculate the slope.
    \addplot[only marks, mark=*, blue] coordinates {
        (1,1.5)
        (5,3.5)
    };

    % Draw the right triangle in data coordinates.
    \draw[red, thick]
        (axis cs:1,1.5) -- (axis cs:5,1.5) --
        (axis cs:5,3.5) -- cycle;

    % Label the horizontal and vertical legs.
    \node[below] at (axis cs:3,1.5) {$\Delta x=4$};
    \node[right] at (axis cs:5,2.5) {$\Delta y=2$};

    % State the resulting slope.
    \node[above] at (axis cs:3,2.5) {$m=\dfrac{\Delta y}{\Delta x}=\dfrac{1}{2}$};
\end{axis}
\end{tikzpicture}

\end{document}

This is a complete LaTeX document; the pgfplots package also loads TikZ. The compat setting selects a PGFPlots compatibility level. Change the line, points, axis limits, and labels to match your graph.

How the triangle is positioned

The two selected points are the ends of the line segment that forms the triangle’s hypotenuse. The third corner, (5,1.5)(5,1.5), combines the second point’s xx-coordinate with the first point’s yy-coordinate. Thus the three corners are (1,1.5)(1,1.5), (5,1.5)(5,1.5), and (5,3.5)(5,3.5).

The drawing command connects those corners and closes the path. The cycle path operation draws the last side back to the starting point. In this example, that closing side lies on top of the plotted line, so it appears red. If you want the plotted line to remain visible in its original color, draw only the two legs instead:

\draw[red, thick]
    (axis cs:1,1.5) -- (axis cs:5,1.5) -- (axis cs:5,3.5);

The line plot then supplies the hypotenuse.

The labels are separate nodes located in data coordinates. For example, \node[below] at (axis cs:3,1.5) centers the run label below the midpoint of the horizontal leg. Adjust the node anchors (below, right, above, or left) or the node coordinates if labels overlap other plot elements.

Calculate and label the slope

For points (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2) with x2≠x1x_2\ne x_1, the slope is

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

If x2=x1x_2=x_1, the line is vertical and its slope is undefined. Here, m=(3.5−1.5)/(5−1)=2/4=1/2m=(3.5-1.5)/(5-1)=2/4=1/2. Keep the subtraction order consistent in the numerator and denominator. Reversing the order for both points gives the same slope; reversing it for only one gives the wrong sign.

You can use other points on the same straight line. For a clearer diagram, choose points far enough apart that the legs and their labels are easy to see, but keep all three corners within the visible axis limits.

Common mistakes

  • Using plain TikZ coordinates inside an axis. A coordinate like (1,1.5) is not the reliable way to specify a data point inside a PGFPlots axis. Use (axis cs:1,1.5) so the values are interpreted in axis units.
  • Making the triangle’s corners inconsistent. For a horizontal-then-vertical triangle, the intermediate corner must pair one endpoint’s xx-value with the other endpoint’s yy-value. Otherwise, the legs may not meet at a right angle or the hypotenuse may not match the plotted segment.
  • Reading slope from the apparent angle. The visual steepness depends on the plot’s width, height, and axis ranges. Calculate slope from the data-coordinate differences, not from how steep the line looks on the page.
  • Putting labels outside the visible region. A node may be clipped at the axis boundary. Move it inward, expand the axis limits, or use an appropriate clipping setting if an outside label is intentional.

A note about unequal axis scales

The triangle represents the correct run and rise even if one unit on the xx-axis takes a different physical distance on the page than one unit on the yy-axis. The horizontal and vertical directions remain perpendicular, so the triangle still appears right-angled. Unequal scales change the displayed leg lengths relative to their data values and the apparent angle of the line, so the drawn line’s angle may not visually correspond to its numerical slope. This does not change the calculation: the slope is still Δy/Δx\Delta y/\Delta x in data units.

References

  1. PGFplots — Manual for Package PGFplots

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