如何在 matplotlib 中获得一个开放和缩放的箭头

How to get an open and scaling arrow head in matplotlib

在 Seaborn 条形图中,我想用箭头注释一列。现在,虽然我看到这看起来有点挑剔,但我真的很喜欢指向 both

的箭头
  1. 有一个开放的头部(即,不是一个封闭的三角形作为头部,而是两条开放的线)和
  2. 缩放 当我调整图形大小时。

我发现了两种添加箭头的 matplotlib 方法(arrowannotate 方法),但每一种似乎都缺少这些功能之一。此代码并排绘制:

import seaborn as sns

sns.plt.subplot(121)
ax = sns.barplot(("x", "y", "z"), (1, 4, 7))
# head is not open
ax.arrow(0, 5, 0., -3.5, lw=1, fill=False,
         head_length=.5, head_width=.2,
         length_includes_head=True)

sns.plt.subplot(122)
ax = sns.barplot(("x", "y", "z"), (1, 4, 7))
# head does not scale with figure
ax.annotate("", xytext=(0, 5), xy=(0, 1.5),
            arrowprops=dict(arrowstyle="->, head_length = 2, head_width = .5", lw=1))

sns.plt.show()

左箭头的头部是闭合的(丑陋的),但当我调整图形大小时它缩放得很好(因为头部大小是以数据单位为单位,我假设)。右箭头的头部漂亮且开放,但无论图形大小如何,它始终保持相同的像素大小。所以图小的时候,右箭头看起来比较大:

当我把图放大时,右边的箭头变得——相对地——变小,而左边的箭头很好地缩放:

那么,有没有办法可以打开 缩放箭头?

关键是使用 overhang 参数并将其设置为 1 或接近它的值。

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(4,4))

v = [-0.2, 0, .2, .4, .6, .8, 1]
for i, overhang in enumerate(v):
    ax.arrow(.1,overhang,.6,0, width=0.001, color="k", 
             head_width=0.1, head_length=0.15, overhang=overhang)

ax.set_yticks(v)
ax.set_xticks([])
ax.set_ylabel("overhang")
ax.set_ylim(-0.3,1.1)
plt.tight_layout()
plt.show()