如何在Matplotlib中获得打开和缩放的箭头 [英] How to get an open and scaling arrow head in matplotlib
问题描述
在 Seaborn 条形图中,我想用箭头注释一列.现在,虽然我看到这似乎有点眼光,但我还是很希望那个箭头同时指向
In a Seaborn barplot, I want to annotate a column with an arrow. Now, while I see how this might seem a little discerning, I would really like that arrow to both
- 有一个开放的头部(即头部不是一个封闭的三角形,而是两条开放的线)和 调整图的大小时
- scale .
- have an open head (i.e., not a closed triangle as a head, but two open lines) and
- scale when I resize the figure.
我发现了两种添加箭头的matplotlib方法( arrow
和 annotate
方法),但是每种方法似乎都缺少这些功能之一.这段代码并排绘制:
I have found two matplotlib ways of adding arrows (the arrow
and annotate
methods), but each one seems to lack one of these features. This code plots both side by side:
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()
左箭头的头部是闭合的(丑陋的),但是当我调整图形大小时它可以很好地缩放(因为头部大小是数据单位,我假设).右箭头的头部很好而且很开放,但无论图形大小如何,它始终保持相同的像素大小.所以当图很小的时候,右箭头看起来比较大:
The left arrow's head is closed (ugly), but it scales fine when I resize the figure (because the head size is in data units, I assume). The right arrow's head is nice and open, but it always keeps the same pixel size regardless of figure size. So when the figure is small, the right arrow head looks relatively large:
当我放大图形时,右箭头变小-相对-小,而左箭头变好:
And when I make the figure bigger, the right arrow head becomes - relatively - smaller, while the left arrow head scales nicely:
那么,有没有办法拥有一个开放的和缩放箭头?
So, is there a way to have an open and scaling arrow head?
推荐答案
关键是使用 overhang
参数并将其设置为 1 或接近它的值.
The key is to use the overhang
argument and set it to 1 or something close to it.
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()
这篇关于如何在Matplotlib中获得打开和缩放的箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!