219

I am trying to do a scatter plot in matplotlib and I couldn't find a way to add tags to the points. For example:

scatter1=plt.scatter(data1["x"], data1["y"], marker="o",
                     c="blue",
                     facecolors="white",
                     edgecolors="blue")

I want for the points in "y" to have labels as "point 1", "point 2", etc. I couldn't figure it out.

Davide Fiocco
  • 4,639
  • 2
  • 34
  • 63
J. Velazquez-Muriel
  • 2,761
  • 3
  • 17
  • 20

1 Answers1

380

Perhaps use plt.annotate:

import numpy as np
import matplotlib.pyplot as plt

N = 10
data = np.random.random((N, 4))
labels = ['point{0}'.format(i) for i in range(N)]

plt.subplots_adjust(bottom = 0.1)
plt.scatter(
    data[:, 0], data[:, 1], marker='o', c=data[:, 2], s=data[:, 3] * 1500,
    cmap=plt.get_cmap('Spectral'))

for label, x, y in zip(labels, data[:, 0], data[:, 1]):
    plt.annotate(
        label,
        xy=(x, y), xytext=(-20, 20),
        textcoords='offset points', ha='right', va='bottom',
        bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
        arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0'))

plt.show()

enter image description here

joon
  • 3,679
  • 1
  • 37
  • 51
unutbu
  • 777,569
  • 165
  • 1,697
  • 1,613
  • 8
    That's what I was going to say. Link to docs: http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.annotate Link to demo: http://matplotlib.sourceforge.net/examples/pylab_examples/annotation_demo2.html – Paul Feb 28 '11 at 21:06
  • 4
    @ubuntu Is it possible to use numbers (or labels) _instead_ of the points? – Vladtn Feb 29 '12 at 13:40
  • @Vladtn: You can change the annotations by redefining the `labels` variable. – unutbu Feb 29 '12 at 17:01
  • @unutbu I mean _not_ show the coloured circles at all and show numbers instead. No use of _annotate_ – Vladtn Feb 29 '12 at 17:52
  • 16
    @Vladtn: You can remove the circles by omitting `plt.scatter`. You can place arbitrary text on the image using `plt.annotate(label, xy = (x, y), xytext = (0, 0), textcoords = 'offset points')`. Notice `xytext = (0, 0)` means no offset, and omitting `arrowprops` causes `plt.annotate` to not draw an arrow. – unutbu Feb 29 '12 at 18:30
  • I had to modify how the labels went with something like: xy=(x, y),xytext = (x - 20, y + 20) Otherwise it stacked all the labels at one point. – JBWhitmore Jun 26 '14 at 22:52
  • Not sure if anyone is still active but I have used this code (Thank you) however I need the points to start at 1 not 0 and I can't seem to change this :/ I tried changing {0} to {1} but that's not it. I tried +1 to the index but no again :/ – OParker Oct 09 '15 at 16:34
  • 1
    @OParker: Change `'point{0}'.format(i)` to `'point{0}'.format(i+1)`. Or, you could change the `range`: `['point{0}'.format(i) for i in range(N)]` to `['point{0}'.format(i) for i in range(1,N+1)]`. – unutbu Oct 09 '15 at 17:52
  • hey, notice in the figure how the "point3" label is showing although it's not in the plot area anymore? Is there a way to have that not happen? – J Webster Oct 11 '18 at 17:18
  • @JWebster: There probably is (perhaps via clipping?) but off hand, I don't know how to do it. If you post a new question, someone here may be able to show you how. – unutbu Oct 12 '18 at 00:40