在图上标记python数据点


问题内容

我搜索了年龄(小时数,类似于年龄),以找到一个非常烦人的(看似基本的)问题的答案,由于找不到合适的问题,我发布了一个问题并对其进行回答,希望将为别人节省大量我在noobie绘图技能上花费的时间。

如果要使用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轴上,位于30
x轴上位于自己的小区域。

您需要同时绘制i和j的线,否则仅绘制x或y数据标签。

您会得到类似这样的信息(仅注意标签):
我自己的图,数据点标记为

它不理想,仍然存在一些重叠-但总比没有好,这就是我所拥有的。


问题答案:

(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

plt.plot(A,B)
for xy in zip(A, B):                                       # <--
    ax.annotate('(%s, %s)' % xy, xy=xy, textcoords='data') # <--

plt.grid()
plt.show()

在此处输入图片说明