221

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
  • 5,350
  • 5
  • 35
  • 72
Javier
  • 2,818
  • 3
  • 19
  • 20

2 Answers2

384

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,899
  • 1
  • 40
  • 53
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 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
0

Another option could be using plt.text. Here is a reproducible example:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(20)

N = 5
X=np.random.randint(10, size=(N))
Y=np.random.randint(10, size=(N))

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

plt.figure(figsize=(8,6))
plt.scatter(X, Y, s=100, color = "blue")
plt.xlabel("X")
plt.ylabel("Y")
for i, label in enumerate(annotations):
    plt.text(X[i]-0.6, Y[i]+0.3,label, rotation = -45,
            bbox=dict(boxstyle="rarrow,pad=0.3", alpha = 0.3))

plt.show()

Output:

enter image description here

Quinten
  • 35,235
  • 5
  • 20
  • 53