Python 如何在matplotlib中获取打开和缩放箭头
在Seaborn条形图中,我想用箭头注释一列。现在,当我看到这可能看起来有点挑剔的时候,我真的希望这两个箭头Python 如何在matplotlib中获取打开和缩放箭头,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,在Seaborn条形图中,我想用箭头注释一列。现在,当我看到这可能看起来有点挑剔的时候,我真的希望这两个箭头 有一个开放的头部(即不是一个封闭的三角形作为头部,而是两条开放的线)和 调整图形大小时进行缩放 我找到了两种添加箭头的matplotlib方法(arrow和annotate方法),但每种方法似乎都缺少其中一种功能。此代码并排绘制两个图形: import seaborn as sns sns.plt.subplot(121) ax = sns.barplot(("x", "y", "z"
arrow
和annotate
方法),但每种方法似乎都缺少其中一种功能。此代码并排绘制两个图形:
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()
左箭头的头部是闭合的(难看),但当我调整图形大小时,它的缩放效果很好(因为我假设头部大小是以数据单位表示的)。右箭头的头部很漂亮,而且是开放的,但是不管数字大小,它总是保持相同的像素大小。因此,当图形较小时,右箭头看起来相对较大:
当我把图放大时,右边的箭头相对地变小了,而左边的箭头缩放得很好:
那么,有没有一种方法可以打开并缩放箭头?关键是使用
overnang
参数并将其设置为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()
ax.annotate不允许“悬垂”,并给出错误:AttributeError:“FancyArrowPatch”对象没有属性“悬垂”头宽似乎是可以使用的arrowprops组件