apaillarse
apaillarse

Reputation: 85

Label data when doing a scatter plot in python

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

Upvotes: 4

Views: 16375

Answers (2)

apaillarse
apaillarse

Reputation: 85

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

Upvotes: 1

Lucas
Lucas

Reputation: 7341

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

Upvotes: 1

Related Questions