Introduction to Matplotlib and simple use of pyplot-text annotation and arrow, matplotlibpyplot
When you use pyplot to draw a graph, you sometimes need to mark some text on the graph. If the curve is relatively close, you 'd better use arrows to indicate the correspondence between the text and the curve. This section describes how to use text labels and arrows.
Add annotation using pyplot. text, called by pyplot or subplot. The following are optional parameters,
text(tx,ty,fontsize=fs,verticalalignment=va,horizontalalignment=ha,...)
Where, tx and ty specify the location where the text is placed, va and ha specify the method, which can be top, bottom, center or left, right, center, or text with a border, the border shape can also be an arrow with a specified direction.
Add the arrow to use pyplot. annotate. The call method is similar to text. The following are optional parameters,
annotate(text,xy=(tx0,ty0),xytext=(tx1,ty1),arrowprops=dict(arrowstyle="->",connectionstyle="arc3"))
Here, text is the text with the arrow, xy is the position and end of the arrow, xytext is the start point, and arrowtypes specifies the arrow style. For more information, see the manual.
The effect is as follows,
The Code is as follows, but some modifications have been made based on the previous subplot,
#!/usr/bin/env pythonimport numpy as npimport matplotlib.pyplot as pltdef f1(t): return np.exp(-t)*np.cos(2*np.pi*t)def f2(t): return np.sin(2*np.pi*t)*np.cos(3*np.pi*t)t = np.arange(0.0,5.0,0.02)plt.figure(figsize=(8,7),dpi=98)p1 = plt.subplot(211)p2 = plt.subplot(212)label_f1 = "$f(t)=e^{-t} \cos (2 \pi t)$"label_f2 = "$g(t)=\sin (2 \pi t) \cos (3 \pi t)$"p1.plot(t,f1(t),"g-",label=label_f1)p2.plot(t,f2(t),"r-.",label=label_f2,linewidth=2)p1.axis([0.0,5.01,-1.0,1.5])p1.set_ylabel("v",fontsize=14)p1.set_title("A simple example",fontsize=18)p1.grid(True)#p1.legend()tx = 2ty = 0.9p1.text(tx,ty,label_f1,fontsize=15,verticalalignment="top",horizontalalignment="right")p2.axis([0.0,5.01,-1.0,1.5])p2.set_ylabel("v",fontsize=14)p2.set_xlabel("t",fontsize=14)#p2.legend()tx = 2ty = 0.9p2.text(tx,ty,label_f2,fontsize=15,verticalalignment="bottom",horizontalalignment="left")p2.annotate('',xy=(1.8,0.5),xytext=(tx,ty),arrowprops=dict(arrowstyle="->",connectionstyle="arc3"))plt.show()
This is simple, so don't be too complicated.
Summary
The above is a brief introduction to Matplotlib and the simple use of pyplot-all content of text annotation and arrow, and I hope to help you. Interested friends can continue to refer to this site:
《Use matplotlib to draw a contour map"
《Python uses matplotlib to draw a simple animation instance"
《Matplotlib introduction, installation and simple instance code"
If you have any shortcomings, please leave a message. Thank you for your support!