Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get arrow heads tip to start/end at specified coordinates in Python?

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.

enter image description here

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?

like image 327
Tom Kurushingal Avatar asked Aug 26 '15 17:08

Tom Kurushingal


1 Answers

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

Graph with lines and arrows

like image 103
Amy Teegarden Avatar answered Sep 20 '22 05:09

Amy Teegarden