是否有任何 matplotlib 函数可以在图表中的两点之间创建增长箭头?

问题描述 投票:0回答:2

我想知道是否可以在条形图或折线图中绘制图表两点之间的箭头。类似下图这样:

我一直在寻找类似的东西,但在 stackoverflow 和任何其他网站上都没有找到类似的东西。

非常感谢您的帮助!

python matplotlib charts data-visualization
2个回答
1
投票

没有任何内置的 matplotlib 函数可以执行此操作,但您可以使用 arrowbbox 自行构建注释。您还可以将此代码包装在任意两个数据点之间的函数中,以使其更具通用性。

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


-1
投票

我不明白你是如何在“ax.text”中获得值 240*1.18 的。

你是怎么计算的?

© www.soinside.com 2019 - 2024. All rights reserved.