Python python and Matlab drawing summary __python

Source: Internet
Author: User
Tags cos prepare sin

I. GENERAL Concepts

A graphics system is used to achieve the so-called visualization. Before you learn specific commands, understand what features a visual graphic has, and what the relationships are. With a macro understanding, remember a few core commands, and so on when you encounter specific problems to query the relevant documents or to see other similar graphics code. As we are drawing on paper:

First, we need a blank sheet of paper. This piece of white paper is a so-called figure. We can give this figure a name and write it in the middle of this piece of paper. If this is one of a series of sheets of paper, it might also give it a label, such as Page 1th and 2nd.

Then, on this paper we determine where to put the picture, that is, to determine the axis (axes) This sheet of paper has several axes. The scale range of the axis. Linear or logarithmic. Is square. On both sides of the axis. Wait a minute. In the drawing line area, you can also consider adding a grid.

Below, we can draw in the existing coordinates of the functional curve we need, that is, by a point connected to the line. There are many options for wiring, such as rectangular coordinates, polar coordinates, pie charts, arrow charts, and so on. For these dots or lines, we can control its style: color, such as width, and so on.

Finally, you want to add some descriptive text to the line, such as the physical meaning of the axis, the curve in the graph, the legend of the symbol, the overall title of the graph, the meaning of some points in the diagram, and so on.

Second, the realization of MATLAB

In a simple example, the general method of Matlab drawing is given.

T1=0:0.1:4
T2=0:0.05:4% prepare some data

Figure ()% prepare the white paper
Subplot (211)% sub-graph drawing
Plot (T1,sin (2*pi*t1), '--g* ')% linear, color, dot representation
Title (' sine function demo ')% title text
Xlabel (' time (s) ')
Ylabel (' Votage (MV) ') The text of the%XY axis
Xlim ([0.0,5.0])
Ylim ([ -1.2,1.2]) the interval range of%xy axes
Grid on% plus mesh

Subplot (212)
Plot (T2,exp (-t2), ': R ')
Hold on% keep the previous line

Plot (T2,cos (2*pi*t2), '--b ')

Xlabel (' time ')
Ylabel (' amplitude ')

Effect as shown:

Third, the implementation of Python

Implement the same example above.

The toolkit that Python uses to draw is matplotlib. "Matplotlib is a Python 2D drawing library that offers a variety of graphic and interactive environments for publishing quality across platforms in hard-copy format," says a matplotlib home page. "and" matplotlib try to keep easy things going easy, and make difficult things as easy as possible. ”

Web site (http://matplotlib.sourceforge.net/gallery.html) gives a variety of common and some unusual graphic examples, there are source code. In the use of time, see the graphics you need, find the source code, fill in their own data and description text, a beautiful picture produced. In addition, for 3D mapping, although matplotlib itself does not provide, but the strong add-ons has been added, fully capable of conventional 3D mapping.

The primary user recommends using the Pylab mode, Pylab includes all Matplotlib.pyplot drawing commands, as well as functions in NumPy and Matplotlib.mlab, in this mode, and MATLAB drawing commands and routines are almost exactly the same; advanced users recommend using Matplotlib, you can do more details Control.

Mode one:

From Pylab Import * #引入兼容MATLAB包: Pylab


T1=arange (0.0,4.0,0.1)
T2=arange (0.0,4.0,0.05) #准备一些数据, attention and MATLAB are different

Figure ()
Subplot (211)
Plot (T1,sin (2*pi*t1), '--g* ')

Title (' sine function demo ')
Xlabel (' time (s) ')
Ylabel (' Votage (MV) ')

Xlim ([0.0,5.0])
Ylim ([ -1.2,1.2])
The grid (' on ') #控制网格显示和grid (True) effect. The grid () with no parameters plays a toggle role.

Subplot (212)
Plot (T2,exp (-t2), ': R ')
Hold (' on ') #前一条线保持. The usage is similar to grid.
Plot (T2,cos (2*pi*t2), '--b ')

Xlabel (' time ')
Ylabel (' amplitude ')
Show () #这是和MATLAB很大的不同, the graph will come out when this command is used up.

Effect as shown:

Mode two:

Import Matplotlib.pyplot as Plt
Import NumPy as NP #导入包

T1=np.arange (0.0,4.0,0.1)
T2=np.arange (0.0,4.0,0.05) #准备一些数据

Fig = Plt.figure () #准备好这张纸 and passes the handle to the FIG
Ax1 = Fig.add_subplot (211) #使用句柄fig添加一个子图
Line1, = Plt.plot (T1,np.sin (2*np.pi*t1), '--* ') #绘图, return handle to Line1
Plt.title (' sine function demo ')
Plt.xlabel (' time (s) ')
Plt.ylabel (' Votage (MV) ')
Plt.xlim ([0.0,5.0])
Plt.ylim ([ -1.2,1.2])
Plt.grid (' on ') #以上语句不难理解

# #这种方式的优势和不同在以下语句体现. Because of the introduction of the handle, let us more object-oriented, the idea is more clear. of the Code

# #可读性也更高了.

PLT.SETP (line1,lw=2,c= ' G ') #通过setp函数, set the property of the line with the handle line1, C is the shorthand for color
Line1.set_antialiased (False) #通过line1句柄的set_ * property to set line1 properties
Plt.text (4,0, ' $\mu=100,\\sigma=15$ ') #添加text, note that it can accept latex yo.

Ax2=fig.add_subplot (212)
Plt.plot (T2,np.exp (-T2), ': R ')
Plt.hold (' on ')
Plt.plot (T2,np.cos (2*np.pi*t2), '--b ')

Plt.xlabel (' time ')
Plt.ylabel (' amplitude ')
Plt.show ()

Effect as shown:


Related Article

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.