5

I would like to annotate 2 points with the same annotation and I would like to have 2 arrows pointing from that 1 annotation to the points.

I have the following code:

import matplotlib.pyplot as plt

plt.plot( [1,2], [1,2], 'o' )
plt.xlim( [0,3] )
plt.ylim( [0,3] )

plt.annotate( 'Yet another annotation', xy=(1,1),
              xytext=(1.5, .5),
              arrowprops=dict( arrowstyle="->" )
            )
plt.annotate( 'Yet another annotation', xy=(2,2),
              xytext=(1.5, .5),
              arrowprops=dict( arrowstyle="->" )
            )

plt.show()

As you can see in the resulting figure, it works (although it might not be the most elegant way since I have two annotations at the exact same location).

Annotation

I am, however, unhappy with that: I would like to let the arrows start at the exact same position, currently they seem to start from the center of the annotation, I would prefer to let them start from somewhere at the edge of the annotation so that their starting points connect.

How do I achieve that?

Alf
  • 1,821
  • 3
  • 30
  • 48

1 Answers1

10

You can annotate the starting point of the arrows. I.e. let the two arrows start at the same position with no annotating text, and use a third annotation without arrow to annotate this starting point with the text.

import matplotlib.pyplot as plt

plt.plot( [1,2], [1,2], 'o' )
plt.xlim( [0,3] )
plt.ylim( [0,3] )

plt.annotate( "", xy=(1,1), xytext=(1.3, 2.5),
              arrowprops=dict( arrowstyle="->" ) )
plt.annotate( '', xy=(2,2), xytext=(1.3, 2.5),
              arrowprops=dict( arrowstyle="->" ) )
plt.annotate( 'Some annotation', xy=(1.3, 2.5),
              xytext=(1.3, 2.5) , va = "bottom", ha="center" )

plt.annotate( "", xy=(1,1), xytext=(1.5, .5),
              arrowprops=dict( arrowstyle="->",shrinkA=0 ) )
plt.annotate( '', xy=(2,2), xytext=(1.5, .5),
              arrowprops=dict( arrowstyle="->",shrinkA=0 ) )
plt.annotate( 'Yet another annotation', xy=(1.5, .5),
              xytext=(1.5, .5) , va = "top", ha="left" )

plt.show()

enter image description here

You may use the arrowprops key shrinkA to adjust the shrinkage of the arrow, setting it to zero lets the two arrows connect.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712