pythonmatplotlibtextscatter-plotannotate

Scatter plot with different text at each data point


I am trying to make a scatter plot and annotate data points with different numbers from a list. So, for example, I want to plot y vs x and annotate with corresponding numbers from n.

y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
x = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [58, 651, 393, 203, 123]
ax = fig.add_subplot(111)
ax1.scatter(z, y, fmt='o')

Any ideas?


Solution

  • I'm not aware of any plotting method which takes arrays or lists but you could use annotate() while iterating over the values in n.

    import matplotlib.pyplot as plt
    x = [0.15, 0.3, 0.45, 0.6, 0.75]
    y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
    n = [58, 651, 393, 203, 123]
    
    fig, ax = plt.subplots()
    ax.scatter(x, y)
    
    for i, txt in enumerate(n):
        ax.annotate(txt, (x[i], y[i]))
    

    There are a lot of formatting options for annotate(), see the matplotlib website:

    enter image description here