Python and MATLAB plot summary

Source: Internet
Author: User

Python and MATLAB plot summary

I. general concepts

The graphic system is used to achieve the so-called visualization. Before learning specific commands, you must first understand the characteristics of a visual image and the relationship between these features. With a macro understanding, remember several core commands. When you encounter a specific problem, you can query relevant documents or view code similar to other people's graphics. In comparison, we plot data on paper:

First, we need a blank sheet of white paper. This White Paper is a so-called figure. We can give this figure a name written in the middle of the paper. If this is one of a series of sheets, you may also give it a label, such as 1st pages and 2nd pages.

Then, on this piece of paper, determine the position of the picture, that is, how many axes does the paper have? What is the scale range of the coordinate axis? Linear or logarithm? Is it square? What are the two coordinate axes? And so on. In the draw line area, you can also consider adding a grid.

Next, we can draw the required functional curve in the existing coordinates, that is, the line connecting points. There are many ways to connect, such as Cartesian coordinate system, polar coordinate, pie chart, and pie chart. We can control the style of these points or lines, such as color and width.

Finally, you need to add some explanatory text to the line, such as the physical meaning of the coordinate axis, the curve in the graph, the symbol legend, the overall title of the graph, and the meaning of some points in the graph.

II. Implementation of MATLAB

A simple example is used to give a general method of MATLAB plotting.

T1 =.
T2 = 0: 0. 05:4 % Prepare some data

Figure () % Prepare the White Paper
Subplot (211) % Subgraph plot
Plot (t1, sin (2 * pi * t1), '-- g *') % Linear, color, and point representation
Title ('sine function demo ') % Title text
Xlabel ('time (s )')
Ylabel ('votage (mV )') % XY axis text
Xlim ([0.0, 5.0])
Ylim ([-1.2, 1.2]) % XY axis range
Grid on % Add Grid

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

Plot (t2, cos (2 * pi * t2), '-- B ')

Xlabel ('time ')
Ylabel ('samples ')

 

 

Iii. Python implementation

Implement the same example above.

The Python toolkit for drawing is matplotlib. Translated into a matplotlib homepage: "matplotlib is a Python 2D drawing library that provides multiple cross-platform hard copy formats for publishing quality graphics and interactive environments ." "Matplotlib strives to make it easier for easy things to continue, so that difficult things can be as easy as possible ."

Web site (http://matplotlib.sourceforge.net/gallery.html) provides a variety of common and some of the less common graphics instances, there are source code. When you use it, you can see the graph you need, find the source code, and fill in your own data and instructions. A beautiful graph will be generated! In addition, although matplotlib itself is not provided for 3D plotting, the powerful add-ons has been added, which is fully qualified for conventional 3D plotting.

We recommend that you use the pylab mode for beginners. The pylab includes matplotlib. all pyplot drawing commands, as well as numpy and matplotlib. in this mode, functions in mlab are almost identical to MATLAB's drawing commands and routines. Advanced users are advised to use matplotlib for more details.

Method 1:

From pylab import * # Introduce compatible MATLAB package: pylab


T1 = arange (0.0, 4.0, 0.1)
T2 = arange (0.0, 4.0, 0.05) # Prepare some data. Note that it is different from MATLAB.

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])
Grid ('on ') # Control grid display is the same as grid (True) display. Grid () without parameters plays the role of toggle.

Subplot (212)
Plot (t2, exp (-t2), ': R ')
Hold ('on ') # The first line is retained. The usage is similar to that of grid.
Plot (t2, cos (2 * pi * t2), '-- B ')

Xlabel ('time ')
Ylabel ('samples ')
Show () # This is very different from MATLAB. Only when this command is used up will the graphics come out.

 

 

Method 2:

Import matplotlib. pyplot as plt
Import numpy as np # Import package

T1 = np. arange (0.0, 4.0, 0.1)
T2 = np. arange (0.0, 4.0, 0.05) # Prepare some data

Fig = plt. figure () # Prepare the paper and pass the handle to fig
Ax1 = fig. add_subplot (211) # Use the handle fig to add a subgraph
Line1, = plt. plot (t1, np. sin (2 * np. pi * t1 ),'--*') # Drawing, returning the 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 ') # The preceding statements are not hard to understand

# The advantages and differences of this method are embodied in the following statements. The introduction of handles makes us more object-oriented and clearer. Code

# Higher readability.

Plt. setp (line1, lw = 2, c = 'G ') # Use the setp function to set the line property of the line1 handle. c is short for color.
Line1.set _ antialiased (False) # Set the properties of line1 through the set _ * attribute of the line1 handle
Plt. text (100, '$ mu =, \ sigma = 15 $ ') # Add text. Note that it can accept LaTeX!

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 ('amplate ')
Plt. show ()

 

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.