Han*_*ans 3 python matplotlib seaborn
在Seaborn的小图中,我想用箭头注释一列。现在,虽然我看到这看起来似乎有点眼光,但我还是很希望这两个箭头
我发现了两种添加箭头的matplotlib方法(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()
Run Code Online (Sandbox Code Playgroud)
左箭头的头部关闭(难看),但是当我调整图形大小时,它的缩放比例很好(因为我认为头部的大小以数据单位为单位)。右箭头的头部很好看而且很张开,但是无论图形大小如何,它始终保持相同的像素大小。因此,当数字较小时,右箭头看起来相对较大:
当我将图形变大时,右箭头变小-相对-而左箭头变好了:
因此,有没有一种方法可以使用开放且可缩放的箭头?
关键是使用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()
Run Code Online (Sandbox Code Playgroud)