在绘图上标记 python 数据点 [英] Label python data points on plot
问题描述
我搜索了年龄(就像年龄一样的小时数)以找到一个非常烦人(看似基本的)问题的答案,并且因为我找不到一个非常符合答案的问题,所以我发布了一个问题并在希望它可以为其他人节省我刚刚花在我的 noobie 绘图技巧上的大量时间.
I searched for ages (hours which is like ages) to find the answer to a really annoying (seemingly basic) problem, and because I cant find a question that quite fits the answer I am posting a question and answering it in the hope that it will save someone else the huge amount of time I just spent on my noobie plotting skills.
如果你想使用 python matplotlib 标记你的绘图点
If you want to label your plot points using python matplotlib
from matplotlib import pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
A = anyarray
B = anyotherarray
plt.plot(A,B)
for i,j in zip(A,B):
ax.annotate('%s)' %j, xy=(i,j), xytext=(30,0), textcoords='offset points')
ax.annotate('(%s,' %i, xy=(i,j))
plt.grid()
plt.show()
我知道 xytext=(30,0) 与 textcoords 一起使用,您使用那些 30,0 值来定位数据标签点,因此它在 0 y 轴上,而在 x 轴上则是 30 over 本身小区域.
I know that xytext=(30,0) goes along with the textcoords, you use those 30,0 values to position the data label point, so its on the 0 y axis and 30 over on the x axis on its own little area.
您需要绘制 i 和 j 的两条线,否则您只绘制 x 或 y 数据标签.
You need both the lines plotting i and j otherwise you only plot x or y data label.
你会得到这样的结果(只注意标签):
You get something like this out (note the labels only):
它并不理想,仍然有一些重叠 - 但总比没有好,这就是我所拥有的..
Its not ideal, there is still some overlap - but its better than nothing which is what I had..
推荐答案
一次打印 (x, y)
怎么样.
from matplotlib import pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
A = -0.75, -0.25, 0, 0.25, 0.5, 0.75, 1.0
B = 0.73, 0.97, 1.0, 0.97, 0.88, 0.73, 0.54
ax.plot(A,B)
for xy in zip(A, B): # <--
ax.annotate('(%s, %s)' % xy, xy=xy, textcoords='data') # <--
ax.grid()
plt.show()
这篇关于在绘图上标记 python 数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!