Label data when running scatter plot in python

I want to tag every point that I drew in python, and I have not found a suitable way to do this.

Assuming that I have two lists of elements ncalled aand b, I print them as follows:

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

I want each dot to indicate "Variable k" from kfrom 1to n. thank you for your time

+4
source share
2 answers

Here is the best way to do this, 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()

Additional information: Matplotlib: how to place individual tags for a scatter plot

0
source

label

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

+1

Source: https://habr.com/ru/post/1665028/


All Articles