Hans
Hans

Reputation: 2615

How to get an open and scaling arrow head in matplotlib

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:

Right arrow head too big

And when I make the figure bigger, the right arrow head becomes - relatively - smaller, while the left arrow head scales nicely:

Right arrow head too small

So, is there a way to have an open and scaling arrow head?

Upvotes: 4

Views: 12086

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339210

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

enter image description here

Upvotes: 11

Related Questions