Plot Annotation
A plot is only as useful as the text around it. Plot annotation is the set of titles, axis labels, tick labels, legends and pointed notes that let a reader decode a figure without asking you what it shows. In scientific computing you will produce plots constantly, for lab reports, for debugging and for published results. A handful of annotation choices decides whether a figure works as evidence or leaves the reader guessing. This lesson covers what to label, how to place text exactly where you want it, and how a logarithmic scale changes where things land.
What a reader needs from the figure
A reader scanning a figure asks four questions: what was measured, in what units, which curve is which, and what should I notice? Each annotation element answers one of them.
Element | Question it answers | Matplotlib call |
|---|---|---|
Title or caption | What does this figure show? | ax.set_title(...) |
Axis labels | Which quantity, in which unit? | ax.set_xlabel(...), ax.set_ylabel(...) |
Tick labels | What value sits at each mark? | ax.set_xticks(...) |
Legend | Which series is which? | label= in each plot call, then ax.legend() |
Annotation | Which feature matters? | ax.annotate(...) |
An axis label names the quantity, then gives its unit in parentheses: Time (s), Temperature (°C). The unit in the label must describe the numbers actually plotted. If your array holds pascals and you want kilopascals on the axis, divide the data by 1000 before plotting. Editing only the label makes every tick value wrong by a factor of 1000. Greek letters and subscripts can be written with Matplotlib's mathtext, so a label can read τ or Vout instead of tau or Vout.
An annotation is a piece of text, usually joined to an arrow, that is attached to one point on the plot to direct attention to a single feature such as a peak, a threshold crossing or a time constant. Use one or two per figure. A plot covered in arrows hides the very features the arrows are meant to show.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(t, T_model, "-", label="Model")
ax.plot(t_meas, T_meas, "o", label="Measured")
ax.set_title("Cooling of a copper block")
ax.set_xlabel("Time (s)")
ax.set_ylabel("Temperature (°C)")
ax.set_xlim(0, 60)
ax.set_ylim(20, 100)
ax.legend(loc="upper right")
ax.annotate("Time constant, 20 s", xy=(20, 45.8), xytext=(0.48, 0.63),
textcoords="axes fraction", arrowprops={"arrowstyle": "->"})Placing text: data versus axes coordinates
In the call above, xy=(20, 45.8) is given in data coordinates, the same units as the plotted values: 20 s and 45.8 °C. The text position uses axes coordinates, fractions of the plotting area that run from 0 at the left and bottom edges to 1 at the right and top edges. On a linear axis you convert between them with
where xmin and xmax are the axis limits and fx is the axes fraction. The same rule applies vertically with the y-limits.
Example: On an axis set to 0–60 s, an event at 45 s sits at fx=(45−0)/(60−0)=0.75, three-quarters of the way across.
Property | Data coordinates | Axes coordinates |
|---|---|---|
Units | Same as the plotted values | Fraction of the plotting area, 0 to 1 |
After changing axis limits | Moves with the data | Stays at the same spot on screen |
Typical use | Arrow tip on a data feature | Panel letter or note in a corner |
Matplotlib setting | xycoords="data" (the default) | textcoords="axes fraction" or transform=ax.transAxes |
Logarithmic axes
When values span several orders of magnitude, a linear axis crushes the small values against one edge. A logarithmic axis, set with ax.set_yscale("log"), places equal ratios at equal distances, so each factor of 10, one decade, gets the same length. Position on a log axis follows the linear rule applied to logarithms:
Example: On an axis from 1 to 1000, the value 10 sits at fx=(1−0)/(3−0)=0.333, one-third of the way across, whereas a linear axis with the same limits would put it at about 0.009, almost on the left edge.
Limits on a log axis must be positive: zero and negative values have no logarithm, so set limits such as 0.01 to 100 rather than 0 to 100. A log scale also reveals structure. An exponential decay y=y0e−t/τ becomes a straight line on a log y-axis, because log10y falls linearly with t.
What to remember
- Label each axis with the quantity and its unit, and scale the data so the unit is true.
- A legend lists only series plotted with label=.
- Data coordinates follow the data; axes coordinates run from 0 to 1 across the plotting area.
- On a linear axis f=(x−xmin)/(xmax−xmin); on a log axis apply the same rule to log10 values.
- Log-axis limits must be positive, and exponentials plot as straight lines on a log y-axis.
Example: Annotating a capacitor discharge plot
A capacitor charged to 12.0 V discharges through a resistor with time constant τ=2.0 ms, so its voltage is V(t)=12.0e−t/τ V. The array t holds sample times in seconds, from 0 to 0.010 s, and v holds voltages in volts. You start with this code:
fig, ax = plt.subplots()
ax.plot(t, v, label="Measured")
ax.set_ylim(0, 12)a) Label the time axis in milliseconds with ticks every 2 ms. What must you do to the data, and where do the ticks go?
A millisecond is one-thousandth of a second, so every time value must be multiplied by ________ before plotting. Plotting t * 1000 makes the last sample, 0.010 s, appear at ________ on the axis. Then label and tick the axis:
- ax.plot(t * 1000, v, label="Measured") replaces the original plot call.
- ax.set_xlabel("Time (ms)") and ax.set_ylabel("Voltage (V)") state each quantity and its unit.
- ax.set_xlim(0, 10) and ax.set_xticks([0, 2, 4, 6, 8, 10]) give ticks every 2 ms.
The axis carries six ticks, at 0, 2, 4, 6, 8 and 10 ms. Changing only the label would leave the tick labels reading 0.000 to 0.010 under a label that says ms, a factor-of-1000 error.
b) Add an annotation whose arrow points at the curve at t=τ, with its text placed at axes coordinates (0.45, 0.70). Find the arrow's target and the text's position, both in data coordinates.
At t=τ=2.0 ms the exponent is −1, and e−1 is ________, so
The arrow target is (2.0 ms, 4.41 V). After part a the time axis runs from 0 to 10 ms, and ax.set_ylim(0, 12) gives the voltage axis a span of ________. Inverting the axes-fraction rule, x=xmin+fx(xmax−xmin), gives
The text sits at (4.5 ms, 8.4 V), above and to the right of the target. The curve at 4.5 ms is only 12.0e−2.25=1.26 V, so the text is well clear of the data. The call is ax.annotate("t = τ", xy=(2.0, 4.41), xytext=(0.45, 0.70), textcoords="axes fraction", arrowprops={"arrowstyle": "->"}).
c) You switch the voltage axis to a log scale with limits 0.01 V to 100 V. Where does the annotated point now sit as a vertical axes fraction, does the text placement from part b still work, and what shape does the curve take?
Call ax.set_yscale("log") and ax.set_ylim(0.01, 100); the old lower limit of 0 is not allowed because a log axis cannot include zero. The axis spans log10100−log100.01=2−(−2), which is ________ decades. For the target, log104.41 is ________, so
The point now sits at a vertical axes fraction of 0.661, up from 4.41/12=0.368 on the linear axis. Because the text was placed in axes coordinates, it did not move: it is still at 0.70, only about 0.04 of the axis height above the point, so the arrow becomes short and nearly horizontal. Moving the text to (0.45, 0.85) restores a clear arrow.
On the log axis the decay is a straight line, because
falls at 1/(2.0×2.303)=0.217 decades per millisecond. The last sample, 12.0e−5=0.0809 V at 10 ms, stays above the 0.01 V lower limit, so the whole curve is visible.
When you place text on a plot, a position measured in the same units as the plotted values is said to be in coordinates. A position measured as a fraction of the plotting area is in axes coordinates, where the bottom edge of the plotting area is at and the top edge is at , whatever the axis limits are.
You are preparing a scientific plot for a printed laboratory report. Match each formatting or export term to its meaning so that you can choose the appropriate setting.
A file representation that stores lines and text as geometric descriptions rather than only a pixel grid
The intended width and height on the printed page
The number of image pixels per inch at a specified printed size
The size of text measured in points
Font size
Vector output
Physical dimensions
Raster resolution