I am trying to plot a lines with arrow heads on both ends using matplotlib's annotation. But when I plot them, the arrow head tips do not start or end at the specified coordinates as shown in figure. The tips should start and end at 0.6 and 0.8 but they do not.
Reproducible code
import matplotlib.pyplot as plt
fig = plt.figure(figsize = (5, 5))
plt = plt.subplot(111)
plt.axvline(0.6)
plt.axvline(0.8)
plt.axhline(0.6)
plt.axhline(0.8)
plt.annotate('', xy = (0.6, 0.33), xycoords = 'axes fraction', \
xytext = (0.8, 0.33), textcoords = 'axes fraction', fontsize = 7, \
color = '#303030', arrowprops=dict(edgecolor='black', arrowstyle = '<->'))
plt.annotate('', xy = (0.33, 0.6), xycoords = 'axes fraction', \
xytext = (0.33, 0.8), textcoords = 'axes fraction', fontsize = 7, \
color = '#303030', arrowprops=dict(edgecolor='black', arrowstyle = '<->'))
fig.savefig('arrow_head.pdf')
Why does this happen? And how to get the tips to start or end at the respective coordinates?
According to the documentation here, the path is shrunk by the parameters given in shrinkA
and shrinkB
, presumably to provide a little spacing when the arrow is pointing at something. The default value is 2, so if you set them to 0, the spacing should go away. Like so:
plt.annotate('', xy = (0.6, 0.33), xycoords = 'axes fraction', \
xytext = (0.8, 0.33), textcoords = 'axes fraction', fontsize = 7, \
color = '#303030', arrowprops=dict(edgecolor='black', arrowstyle = '<->', shrinkA = 0, shrinkB = 0))
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