Add Labels and Text to Matplotlib Plots: Annotation Examples

Last updated:
Table of Contents

WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.

Line plot with labelled points

import matplotlib.pyplot as plt
import numpy as np


# using some dummy data for this example
xs = np.arange(0,10,1)
ys = np.random.normal(loc=3, scale=0.4, size=10)

# 'bo-' means blue color, round points, solid lines

# zip joins x and y coordinates in pairs
for x,y in zip(xs,ys):

    label = "{:.2f}".format(y)

    plt.annotate(label, # this is the text
                 (x,y), # this is the point to label
                 textcoords="offset points", # how to position the text
                 xytext=(0,10), # distance from text to points (x,y)
                 ha='center') # horizontal alignemtn can be left, right or center


labelled-line-plot-matplotlib Labels contain the value of Y, with 2 decimal places

Bar plot with annotations


Scatter plot with labelled points



Dialogue & Discussion