8

I am trying to adjust the headwidth of an arrow in Matplotlib.

Here's a working code:

import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0,2*np.pi,500)
y = np.sin(t)

fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111)
ax.plot(t,y)

ax.annotate('', xy=(1, -1), xytext=(2, 0),
arrowprops=dict(arrowstyle='<->', facecolor='black'))

Sine plot with double-headed arrow

And it plots a nice-looking double headed arrow as shown. Now when I want to alter the headwidth by doing:

ax.annotate('', xy=(1, -1), xytext=(2, 0),
arrowprops=dict(arrowstyle='<->', facecolor='black',headwidth=10))

or

ax.annotate('', xy=(1, -1), xytext=(2, 0), 
arrowprops=dict(arrowstyle='<->', facecolor='black',head_width=10))

The error returned is:

AttributeError: Unknown property headwidth

or

AttributeError: Unknown property head_width

Is there any way out?

user
  • 5,370
  • 8
  • 47
  • 75
Harry MacDowel
  • 843
  • 1
  • 10
  • 17

1 Answers1

19

When you specify an arrowstyle in your arrowprops dict, you get in instance of a FancyArrowPatch rather than YAArrow, which takes different keywords (but, you probably knew that given your attempt to use head_width). What is unintuitive from the documentation is that when you specify the arrowstyle, which has default head settings, and etc., you modify those particular settings in the arrowstyle string. This is the relevant line from the documentation here (emphasis mine):

The arrowstyle describes how the fancy arrow will be drawn. It can be string of the available arrowstyle names, with optional comma-separated attributes, or one of the ArrowStyle instance.

If you change your dictionary to the following it will work:

arrowprops=dict(arrowstyle='<->, head_width=10', facecolor='black')

Notice that the head_width specification is in the string following a comma after the style. Aside: 10 is a rather large number for that one .... 0.5 might be better!

Ajean
  • 5,528
  • 14
  • 46
  • 69
  • 4
    Wow great stuff, @Ajean! That is one *subtle* documentation! I couldn't get my head to it until you pointed it out. Now I can edit my arrows! Thanks for saving my day!!!!! – Harry MacDowel Dec 23 '14 at 02:12
  • @Ajean I am having a similar error with `ncol` and `fancybox`. I could not find this information in the documentation, where can I find more information about related errors? –  Mar 15 '18 at 08:51
  • @mikey I presume that you mean `ncol` with `legend`, because there is no such thing as an `ncol` keyword for `FancyBboxPatch`, and I don't think there ever has been. Just check the documentation for `legend` and make sure you are following it. Then if you still have issues, please ask a new question. – Ajean Mar 15 '18 at 20:36