简体   繁体   English

在python中执行散点图时标记数据

[英]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. 我想标记我在python中绘制的每个点,我没有找到合适的方法来做到这一点。

Assuming I have two lists of n elements called a and b , I print them this way : 假设我有两个名为abn元素列表,我会这样打印:

    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. 我想用“变量k”标记每个点,其中k显然在1n Thanks for your time 谢谢你的时间

You can use the label plot parameter 您可以使用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()

在此输入图像描述

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 更多信息: Matplotlib:如何为散点图放置单个标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM