6

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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Is va short for vertical alignment and ha for horizontal alignment ?
That is correct. You can actually use horizontalalignment = "left" as well, it's just very long to type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.