在图上标记数据点
- 2025-02-21 08:48:00
- admin 原创
- 28
问题描述:
如果您想使用 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)
与您使用这些 30,0 值来定位数据标签点相一致textcoords
,因此它位于y=0
并且x=30
位于它自己的小区域上。
您需要绘制两条线i
,j
否则您只需绘制图表x
或y
数据标签。
您会得到类似这样的结果(仅注意标签):
这并不理想,仍然存在一些重叠。
解决方案 1:
(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()
解决方案 2:
我遇到了类似的问题并最终得到以下结果:
对我来说,这样做的好处是数据和注释不会重叠。
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()
使用文本参数会.annotate
导致文本位置不理想。在图例和数据点之间画线很麻烦,因为图例的位置很难确定。
解决方案 3:
如果不需要箭头,text()
也可以用来标记点。
import matplotlib.pyplot as plt
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]
fig, ax = plt.subplots()
ax.plot(A,B)
for x, y in zip(A, B):
ax.text(x, y, f"({x}, {y})", fontsize=8)
您还可以通过有条件地注释点来注释某些点或更改标签相对于点的位置。此外,您可以分配任意标签。
例如,以下代码在点左侧绘制标签,否则x>0
在右侧绘制标签。此外,annotate()
允许使用其他 kwargs 来美化标签。
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
labels = 'ABCDEFG'
fig, ax = plt.subplots()
ax.plot(A,B)
# annotator function that draws a label and an arrow
# that points from the label to its corresponding point
def annotate(ax, label, x, y, xytext):
ax.annotate(label, xy=(x,y),
xytext=xytext, textcoords='offset points',
fontsize=15,
arrowprops={'arrowstyle': '-|>', 'color': 'black'})
# conditionally position labels
for label, x, y in zip(labels, A, B):
if y > 0.9:
annotate(ax, label, x, y, (-5, -40))
else:
annotate(ax, label, x, y, (-5, 30))
相关推荐
热门文章
项目管理软件有哪些?
- 2025年20款好用的项目管理软件推荐,项目管理提效的20个工具和技巧
- 2024年开源项目管理软件有哪些?推荐5款好用的项目管理工具
- 2024年常用的项目管理软件有哪些?推荐这10款国内外好用的项目管理工具
- 项目管理软件有哪些?推荐7款超好用的项目管理工具
- 项目管理软件有哪些最好用?推荐6款好用的项目管理工具
- 项目管理软件哪个最好用?盘点推荐5款好用的项目管理工具
- 项目管理软件排行榜:2024年项目经理必备5款开源项目管理软件汇总
- 项目管理必备:盘点2024年13款好用的项目管理软件
- 项目管理软件有哪些,盘点推荐国内外超好用的7款项目管理工具
- 2024项目管理软件排行榜(10类常用的项目管理工具全推荐)
热门标签
云禅道AD