mcs*_*osh 1 python annotate scatter matplotlib
我通过使用variable=plt.scatter(test1,test2)wheretest1和test2are 对应于 x 和 y 的列表制作了一个散点图。
有没有办法用我创建的字符串列表或可变颜色来注释每个点?
我发现:
for i, txt in enumerate(variablelabel):
variable.annotate(txt, (test1[i],test2[i]))
Run Code Online (Sandbox Code Playgroud)
wherevariablelabel定义为我的字符串列表。不幸的是,这似乎并没有注释我的散点图。
或者,我发现您可以使用以下类似的代码添加箭头:
ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
arrowprops=dict(facecolor='black', shrink=0.05),
ax.set_ylim(-2,2)
plt.show()
Run Code Online (Sandbox Code Playgroud)
但这会产生我不想要的大箭头。我只想要列表中的字符串。
对不起,如果我不是很清楚。
您可以使用字符串列表来注释每个点。使用matplotlib.annotate就是解决方案。但是你调用annotate一个关于matplotlib.collections.PathCollection对象(的结果matplotlib.scatter),而不是matplotlib.axes.Axes对象。
在您的代码中:
variable = plt.scatter(test1, test2)
for i, txt in enumerate(variablelabel):
variable.annotate(txt, (test1[i], test2[i]))
Run Code Online (Sandbox Code Playgroud)
variable是一个matplotlib.collections.PathCollection。而是使用以下内容:
plt.scatter(test1, test2)
for i, txt in enumerate(variablelabel):
plt.annotate(txt, (test1[i], test2[i]))
Run Code Online (Sandbox Code Playgroud)
你应该得到这样的东西:
我希望这能帮到您。
| 归档时间: |
|
| 查看次数: |
7143 次 |
| 最近记录: |