Introduction to Matplotlib and simple use of pyplot-text annotation and Arrow

Source: Internet
Author: User

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, verticalignment = 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()

Well, it's just a simple thing, so don't be too complicated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.