How to add an annotation in Matplotlib

Annotations make a plotted data point explain itself without forcing the reader to search the surrounding chart. In Matplotlib, a callout label can point at the value that matters while the rest of the line, marker, and axis context stays visible.

The Axes.annotate() method anchors text to an xy point, which uses data coordinates by default. Placing xytext with textcoords=“offset points” moves the label away from that point in screen points, so the annotation stays readable when the data range changes.

Use an arrowprops dictionary when the label should visibly point back to the selected point. A small bounding box around the text helps the annotation stand out on exported PNG or SVG files without changing the plotted data.

Steps to add a Matplotlib annotation:

  1. Create a Python script for the annotated plot.
    annotation-add.py
    import matplotlib
    import matplotlib.pyplot as plt
     
    months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
    revenue = [62, 68, 71, 86, 91, 88]
    x = list(range(1, len(months) + 1))
     
    fig, ax = plt.subplots(figsize=(6, 3.5), layout="constrained")
    ax.plot(x, revenue, marker="o", linewidth=2.5, color="#2563eb")
    ax.set_xticks(x, months)
    ax.set_ylabel("Revenue (USD thousands)")
    ax.set_title("Quarterly campaign revenue")
    ax.grid(True, axis="y", color="#e5e7eb")
    ax.set_ylim(55, 100)
     
    annotation = ax.annotate(
        "Peak month\n91k",
        xy=(5, 91),
        xytext=(32, 26),
        textcoords="offset points",
        arrowprops={"arrowstyle": "->", "color": "#1f2937", "linewidth": 1.3},
        bbox={"boxstyle": "round,pad=0.3", "fc": "#fef3c7", "ec": "#92400e"},
        ha="left",
        va="bottom",
    )
     
    fig.savefig("annotation-add.png", dpi=160)
     
    print(f"matplotlib {matplotlib.__version__}")
    print(f"annotation text: {annotation.get_text().replace(chr(10), ' / ')}")
    print(f"annotation xy data: {annotation.xy}")
    print(f"text offset points: {annotation.get_position()}")
    print("saved annotation-add.png")

    xy=(5, 91) anchors the arrow to the May data value. xytext=(32, 26) and textcoords=“offset points” move the label 32 points right and 26 points up from that anchor.

  2. Run the script.
    $ python annotation-add.py
    matplotlib 3.11.0
    annotation text: Peak month / 91k
    annotation xy data: (5, 91)
    text offset points: (32, 26)
    saved annotation-add.png
  3. Open annotation-add.png and confirm the label is offset from the May marker while the arrow points back to the peak value.