4

I want to label every dot I plot in python, and I didn't find a proper way to do it.

Assuming I have two lists of n elements called a and b, I print them this way :

    plt.figure()
    plt.grid()
    plt.plot(a , b , 'bo')
    plt.show()

I want to label every point with "Variable k" with k ranging from 1 to n obviously. Thanks for your time

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
apaillarse
  • 85
  • 1
  • 1
  • 9
  • 1
    Possible duplicate of [How do I set the figure title and axes labels font size in Matplotlib?](http://stackoverflow.com/questions/12444716/how-do-i-set-the-figure-title-and-axes-labels-font-size-in-matplotlib) – Arpit Solanki Dec 27 '16 at 18:01
  • ^^ That is setting names for axis'. I believe he wants to label every individual point. – gold_cy Dec 27 '16 at 18:04
  • What have you tried? Please, ***please*** read [ask] and [mcve]. Stack Overflow has very high quality standards. – noɥʇʎԀʎzɐɹƆ Dec 27 '16 at 18:34
  • You're right Dmitry Polonskiy that's exactly what I want to do – apaillarse Dec 27 '16 at 18:35

2 Answers2

1

You can use the label plot parameter

x = np.random.random(3)
y = np.random.random(3)
z = np.arange(3)
colors = ["red", "yellow", "blue"]
c = ["ro", "yo", "bo"]

for i in z:
    plt.plot(x[i], y[i], c[i], label=colors[i] + ' ' + str(i))
plt.legend()

enter image description here

Lucas
  • 6,869
  • 5
  • 29
  • 44
1

Here is the best way of doing it I found :

plt.figure()
plt.scatter(a,b)
labels = ['Variable {0}'.format(i+1) for i in range(n)]
for i in range (0,n):
    xy=(a[i],b[i])
    plt.annotate(labels[i],xy)
plt.plot()

More infos : Matplotlib: How to put individual tags for a scatter plot

Community
  • 1
  • 1
apaillarse
  • 85
  • 1
  • 1
  • 9