Matplotlib Annotations & Text

๐Ÿ”ค Plain text

ax.text(x, y, "Some label")
ax.text(0.5, 0.5, "Centered", transform=ax.transAxes, ha="center", va="center")   # axes-relative coords (0-1)
ax.text(x, y, "Bold red", fontsize=12, color="red", fontweight="bold")

transform=ax.transAxes = coordinates are fractions of the axes (0,0 = bottom-left, 1,1 = top-right), independent of actual data values. Useful for fixed-position labels regardless of data range.

๐ŸŽฏ annotate โ€” text + arrow pointing at a data point

ax.annotate(
    "Peak value",
    xy=(peak_x, peak_y),               # point being annotated
    xytext=(peak_x + 5, peak_y + 2),      # where the text sits
    arrowprops=dict(facecolor="black", arrowstyle="->")
)
ax.annotate(
    "Important",
    xy=(3, 5), xycoords="data",
    xytext=(0.7, 0.9), textcoords="axes fraction",   # mix data + axes-fraction coordinates
    arrowprops=dict(facecolor="red", shrink=0.05)
)
arrowstyleLook
"->"simple arrow
"-|>"filled arrowhead
"fancy"fat curved arrow
"simple"thick simple arrow

๐Ÿ“ Horizontal / vertical reference lines

ax.axhline(y=0, color="gray", linestyle="--")
ax.axvline(x=5, color="gray", linestyle="--")
ax.axhspan(2, 4, alpha=0.2, color="yellow")      # shaded horizontal band
ax.axvspan(1, 3, alpha=0.2, color="blue")

๐Ÿ–ผ๏ธ Text box styling

ax.text(x, y, "Boxed note", bbox=dict(facecolor="white", edgecolor="black", boxstyle="round,pad=0.5"))

๐Ÿ”ข Data labels on bars

bars = ax.bar(categories, values)
for bar in bars:
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width()/2, height, f"{height}", ha="center", va="bottom")

๐Ÿงฎ Mathtext (LaTeX-like formatting)

ax.set_title(r"$\alpha$ vs $\beta$")
ax.set_xlabel(r"$x^2 + y^2 = r^2$")

Wrap in r"$...$" โ€” raw string + dollar signs enable LaTeX-style math rendering without a full LaTeX install.

๐Ÿ”— Next

Matplotlib Colors & Colormaps ยท Matplotlib Saving & Exporting