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

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?
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With