Label data when doing a scatter plot in python

回眸只為那壹抹淺笑 提交于 2019-12-10 16:16:41

问题


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


回答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()




回答2:


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



来源:https://stackoverflow.com/questions/41349942/label-data-when-doing-a-scatter-plot-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!