没有任何内置的 matplotlib 函数可以执行此操作,但您可以使用 arrow 和 bbox 自行构建注释。您还可以将此代码包装在任意两个数据点之间的函数中,以使其更具通用性。
import matplotlib.pyplot as plt
x=list('abcdefg')
y=[10,20,30,40,50,123,240]
fig, ax = plt.subplots(figsize=(12,8))
ax.bar(x,y)
## add the text above the bars
for idx, value in enumerate(x):
ax.text(value, y[idx]+1, str(y[idx]))
## add the bar style connection patch, padding the y-coordinates
t = ax.annotate("",
xy=(x[0], y[0]+5), xycoords='data',
xytext=(x[-1], y[-1]+5), textcoords='data',
arrowprops=dict(arrowstyle="<-", color="0.5",
shrinkA=5, shrinkB=5,
patchA=None, patchB=None,
connectionstyle="bar,angle=-180,fraction=0.1",
),
)
props = dict(boxstyle='round', facecolor="white")
# place a text box in upper left in axes coords
growth = "600%"
ax.text("d", 240*1.18, growth, bbox=props)
## pretty up the chart by removing axes
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
我不明白你是如何在“ax.text”中获得值 240*1.18 的。
你是怎么计算的?