How to Draw the Same Angles in pgfplots and TikZ
At a glance
- 1. Check axis scaling
Unequal physical lengths for one x-unit and one y-unit change the angle you see.
- 2. Use equal units
For a linear x-y plot, add axis equal image so one unit has the same length in both directions.
- 3. Use axis coordinates
Define the angle’s vertices with axis cs coordinates, then draw the TikZ angle pic.
- 4. Compare rendered geometry
If the axis is intentionally stretched, the displayed angle will differ from its data-coordinate angle.
An angle specified by data coordinates is not necessarily the angle you see on the page. If the x- and y-axes use different physical scales, pgfplots stretches the plotted geometry, changing the displayed angle. A TikZ angle marker can still be drawn inside the plot, but it will follow the rendered positions of its vertices.
For an ordinary linear x-y plot, use axis equal image when you want one x-unit and one y-unit to have the same physical length. Then define the angle’s points in the axis coordinate system and use TikZ’s angle pic.
Why the displayed angle changes
Suppose the x-direction is scaled by centimetres per data unit and the y-direction by . A line with data-coordinate slope is drawn with a visual slope
Unless , its visual direction differs from its direction in data coordinates. This is why the same coordinate points can appear to form different angles in a stretched pgfplots axis and in a TikZ picture with equal x- and y-unit lengths.
Worked example: draw an angle inside an axis
This complete example draws two rays from to and , then marks the angle between them. The rays form an angle of about in the data plane. The equal-unit setting preserves that angle visually.
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{angles,quotes}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=4,
ymin=0, ymax=4,
axis equal image,
axis lines=middle,
xlabel={$x$}, ylabel={$y$},
width=8cm, height=8cm
]
\addplot[thick] coordinates {(1,1) (4,1)};
\addplot[thick] coordinates {(1,1) (2,4)};
\coordinate (O) at (axis cs:1,1);
\coordinate (A) at (axis cs:4,1);
\coordinate (B) at (axis cs:2,4);
\pic[draw, "$\theta$", angle radius=8mm,
angle eccentricity=1.3] {angle=A--O--B};
\end{axis}
\end{tikzpicture}
\end{document}
The axis cs: prefix is important: it tells pgfplots to convert each data point into the corresponding plotted position. The angle=A--O--B syntax marks the angle whose vertex is O, between the rays toward A and B. The angles library provides the pic, and quotes enables the quoted label.
Choose axis dimensions and limits deliberately
axis equal image makes the x- and y-unit lengths equal, so a square range such as to on each axis can be shown without distorting angles. If the axis ranges differ, equal unit lengths require the physical plot dimensions to reflect those ranges. For example, a horizontal range twice as wide as the vertical range needs a plot area twice as wide as it is tall to show equal units.
If the plot must fit a particular rectangular space, you may need to accept unequal unit lengths. In that case, the angle pic marks the angle between the rendered rays, not an undistorted angle in data coordinates. You can instead place the angle marker in a separate TikZ coordinate system with the desired proportions, but it will no longer automatically track the plotted points if the axis changes.
Common mistakes
- Setting only
widthandheight: These specify the plot’s physical dimensions; they do not by themselves guarantee equal x- and y-unit lengths. - Using ordinary TikZ coordinates for plot data: Coordinates such as
(2,3)inside a plot may be interpreted in the current TikZ coordinate system. Useaxis cs:2,3when you mean data values. - Confusing a data angle with a visible angle: An angle computed from data values can differ from the angle seen on a stretched plot.
- Assuming equal scaling fixes every axis type: The discussion above assumes ordinary linear axes. Logarithmic or other nonlinear axis transformations change how data-coordinate directions appear, so a straight angle in the data plane may not remain a straight-line angle after plotting.
If you are annotating a triangle or slope directly on a plot, the same coordinate-system distinction applies: construct the annotation from the plotted positions, and use equal unit scaling when the diagram must preserve geometric angles.
References
- PGFPlots: Manual for Version 1.18.2
- TikZ and PGF Manual
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.