annotate a plot using matplotlib


Question

I am using this to making a plot with contains in specific moment something such as highlight

plt.axis('normal')
plt.axvspan(76, 76, facecolor='g', alpha=1)
plt.plot(ts.e1, 'r',linewidth=1.5)
plt.ylabel("Anger",fontsize=14,color='blue')
plt.ylim(ymax=0.04)
plt.xlim(xmax=122)
plt.grid(True)
plt.title("Anger - Real Events", fontsize=20,color='black')
plt.xlabel('minutes', fontsize=14, color='b')
plt.show()

Is there any idea if i could add horizontal text like 'span 1' in this:

plt.axvspan(76, 76, facecolor='g', alpha=1)

or using the text 'line1' and have a arrow to show in this span?

1
9
3/1/2013 9:19:53 AM

Here's how you can have an arrow showing to the span:

import matplotlib.pyplot as plt

plt.axvspan(76, 76, facecolor='g', alpha=1)
plt.annotate('This is awesome!', 
             xy=(76, 0.75),  
             xycoords='data',
             textcoords='offset points',
             arrowprops=dict(arrowstyle="->"))
plt.show()

For more info about annotate see docs.

The output of the above code: enter image description here

13
12/3/2014 1:44:16 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon