How to get an open and scaling arrow head in matplotlib

强颜欢笑 提交于 2021-02-06 13:53:53

问题


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

  1. have an open head (i.e., not a closed triangle as a head, but two open lines) and
  2. scale when I resize the figure.

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?


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



来源:https://stackoverflow.com/questions/43378730/how-to-get-an-open-and-scaling-arrow-head-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!