Introduction to Matplotlib and simple use of pyplot-local zoom-in

Source: Internet
Author: User

As mentioned above, how can we draw a partial enlarged image, such as the source image on the left and the source image on the right, and use a box or something in the source image, to enlarge the frame, you can use two lines to point to the enlarged graph from the box,
Shows the effect,

This section describes how to draw a box and two lines.
Draw a box and draw four lines connecting the four vertices of the box. From a point around a circle and then back to the starting point, draw a box using pyplot (x, y). You can also specify the color and line width.
Next we will draw a line across subgraphs. Here we use a connection line in matplotlib. patches has a ConnectionPatch type, which is used to draw lines between one or more subgraphs. annotate seems to be able to draw arrows only in one subgraph, so there should be no arrows, that is a line, which has the same effect as connectionPatch.
Con = ConnectionPatch (xyA = xy1, xyB = xy0, coordsA = "data", coordsB = "data ",
AxesA = p1, axesB = p0)
P1.add _ artist (con)
Here, xyA is the point in p1, and xy0 is the point in p0. The default values of coordsA and coordsB are "data". You do not need to change the value. Then, axesA is used to add the subgraph of ConnectionPatch. axesB, the subgraph to be connected. It should be noted that ConnectionPatch is a painting operation that will cover the image. It seems like this means that you do not need to continue using it, and you have no motivation to go further.
Finally, use p1.add _ artist (con) to add the connection line to the subgraph.

Note that raw strings are required when using the formula text. Sometimes r "text" is not used, however, sometimes you need to use r "text" to specify the raw string when using \. For example, if r "\ alpha" is input, it is a raw string if r is not used, there will be problems.

The complete code is as follows,

#!/usr/bin/env pythonimport numpy as npimport matplotlib.pyplot as pltfrom matplotlib.patches import ConnectionPatchdef f1(t):    return np.exp(-t)*np.cos(2*np.pi*t)def f11(t):    return np.exp(-t)*np.cos(2*np.pi*t+0.2)def f111(t):    return np.exp(-t+0.2)*np.cos(2*np.pi*t)t = np.arange(0.0,5.0,0.02)plt.figure(figsize=(16,8),dpi=98)p1 = plt.subplot(121,aspect=5/2.5)p2 = plt.subplot(122,aspect=0.5/0.05)label_f0 = r"$f(t)=e^{-t+\alpha} \cos (2 \pi t+\beta)$"label_f1 = r"$\alpha=0,\beta=0$"label_f11 = r"$\alpha=0,\beta=0.2$"label_f111 = r"$\alpha=0.2,\beta=0$"p1.plot(t,f1(t),"g",label=label_f1,linewidth=2)p1.plot(t,f11(t),"r-.",label=label_f11,linewidth=2)p1.plot(t,f111(t),"b:",label=label_f111,linewidth=2)p2.plot(t,f1(t),"g",label=label_f1,linewidth=2)p2.plot(t,f11(t),"r-.",label=label_f11,linewidth=2)p2.plot(t,f111(t),"b:",label=label_f111,linewidth=2)p1.axis([0.0,5.01,-1.0,1.5])p1.set_ylabel("v",fontsize=14)p1.set_xlabel("t",fontsize=14)#p1.set_title("A simple example",fontsize=18)p1.grid(True)p1.legend()tx = 0.5ty = 0.9p1.text(tx,ty,label_f0,fontsize=15,verticalalignment="top",horizontalalignment="left")p2.axis([4,4.5,-0.02,0.03])p2.set_ylabel("v",fontsize=14)p2.set_xlabel("t",fontsize=14)p2.grid(True)p2.legend()# plot the boxtx0 = 4tx1 = 4.5ty0 = -0.1ty1 = 0.1sx = [tx0,tx1,tx1,tx0,tx0]sy = [ty0,ty0,ty1,ty1,ty0]p1.plot(sx,sy,"purple")# plot patch linesxy=(4.45,0.09)xy2 = (4.02,0.026)con = ConnectionPatch(xyA=xy2,xyB=xy,coordsA="data",coordsB="data",        axesA=p2,axesB=p1)p2.add_artist(con)xy = (4.45,-0.09)xy2 = (4.02,-0.018)con = ConnectionPatch(xyA=xy2,xyB=xy,coordsA="data",coordsB="data",        axesA=p2,axesB=p1)p2.add_artist(con)plt.show()

Well, that's it.

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.