在绘图上标记 python 数据点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22272081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:37:56  来源:igfitidea点击:

Label python data points on plot

pythonmatplotliblabelsannotate

提问by ashley

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.

我搜索了年龄(就像年龄一样的小时数)以找到一个非常烦人的(看似基本的)问题的答案,并且因为我找不到一个非常适合答案的问题,所以我发布了一个问题并回答它希望它将为其他人节省我刚刚花在我的 noobie 绘图技巧上的大量时间。

If you want to label your plot points using python matplotlib

如果您想使用 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()

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.

我知道 xytext=(30,0) 与 textcoords 一起使用,您使用那些 30,0 值来定位数据标签点,因此它位于 0 y 轴上,而 30 则位于其自己的小区域上的 x 轴上。

You need both the lines plotting i and j otherwise you only plot x or y data label.

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

You get something like this out (note the labels only):
My own plot with data points labeled

你会得到这样的东西(只注意标签):
我自己的标有数据点的图

Its not ideal, there is still some overlap - but its better than nothing which is what I had..

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

采纳答案by falsetru

How about print (x, y)at once.

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

enter image description here

在此处输入图片说明

回答by Markus Dutschke

I had a similar issue and ended up with this:

我有一个类似的问题,最后得到了这个:

enter image description here

在此处输入图片说明

For me this has the advantage that data and annotation are not overlapping.

对我来说,这具有数据和注释不重叠的优点。

from matplotlib import pyplot as plt
import numpy as np

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)

# annotations at the side (ordered by B values)
x0,x1=ax.get_xlim()
y0,y1=ax.get_ylim()
for ii, ind in enumerate(np.argsort(B)):
    x = A[ind]
    y = B[ind]
    xPos = x1 + .02 * (x1 - x0)
    yPos = y0 + ii * (y1 - y0)/(len(B) - 1)
    ax.annotate('',#label,
          xy=(x, y), xycoords='data',
          xytext=(xPos, yPos), textcoords='data',
          arrowprops=dict(
                          connectionstyle="arc3,rad=0.",
                          shrinkA=0, shrinkB=10,
                          arrowstyle= '-|>', ls= '-', linewidth=2
                          ),
          va='bottom', ha='left', zorder=19
          )
    ax.text(xPos + .01 * (x1 - x0), yPos,
            '({:.2f}, {:.2f})'.format(x,y),
            transform=ax.transData, va='center')

plt.grid()
plt.show()

Using the text argument in .annotateended up with unfavorable text positions. Drawing lines between a legend and the data points is a mess, as the location of the legend is hard to address.

使用文本参数 in.annotate结束了不利的文本位置。在图例和数据点之间画线是一团糟,因为图例的位置很难定位。