Introduction to Matplotlib and simple use of pyplot

Source: Internet
Author: User

Recently, I had to spend some pictures, which were originally intended to use matlab. However, sometimes matplotlib could not be tolerated by matlab. I am familiar with and like Python, so matplotlib replaces matlab.

After a few days of use, I felt good, but I felt that the document was not full enough. Some links on the web version could not be opened, so there were so many pdf versions. I couldn't find any content, so I went to the source code. Below are some of my experiences. It mainly uses pyplot in matplotlib and related to it.
Let's first look at the simple and draw a simple function curve,

# Import pyplot. The example in this document usually alias it pltimport matplotlib. pyplot as plt # create a graph plt. figure () # plot the curve plt. plot (x, y) # displays plt. show ()

In general, in addition to displaying the curve, there are other requirements, such as the annotation of the coordinate axis, the size of the graph, the title, the legend, and drawing multiple curves in one region, draw multiple subgraphs in a sequence, similar to subplot in matlab. The following is an introduction.

First, let's take a look at plt. figure (). This is to create a graph without any parameters, but also with parameters. You can directly view the source code parameter descriptions,
Figsize = (width, height) # The size of the specified graph, in the unit of inch,
Dpi # dot per inch, pixel density, is it similar to the iphone display's ppi, the ppi of the Retina screen is required at 326ppi, on the 3.5-inch screen of 960x640.
There are other parameters.

In this example, figure () is called with plt, and its return value is not saved. This function has a return value. What is the Figure object returned,

Look at plt again. plot (x, y). This plot the data into a curve and shows that x corresponds to the horizontal coordinate, y corresponds to the vertical coordinate, and x and y are all a one-dimensional list, which is well understood.
In addition to the x and y parameters, the plot can also have other parameters, such as specifying the line style, which can be dotted line, dotted line, color, and line width, these can be specified using keyword parameters. Here, you can specify the label, which can be used as the legend text.

The last is plt. show (). This is to display the image, as if there is nothing to say.

Annotations and titles
Because it is only a graph, it is better to specify it, plt. xlabel (text), plt. ylabel (text), plt. title (text), you can also set the font size, use the keyword parameter fontsize = 16, add the formula input, it supports latex format formula input, it is the formula for writing latex between two $ strings to ensure that the string format is raw, which is very useful. There is an online latex formula editing test, very useful, given the link, http://www.codecogs.com/latex/eqneditor.php

About the coordinate axis range,
Use plt. axis ([xmin xmax ymin ymax] to specify,

For the grid, plt. grid (True) can display the grid.

For legends, if a label is set when the curve is drawn, the legend is also very simple. plt. legend. In matplotlib. There is a dedicated legend class, which is still very complicated and will not be further explored for the moment.

For drawing multiple curves, you need to use hold on in matlab. This does not seem to need to be. simply continue plt. plot (x, y.
The following is a complete example.

import 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()plt.plot(t,f1(t),"g-",label="$f(t)=e^{-t} \cdot \cos (2 \pi t)$")plt.plot(t,f2(t),"r-.",label="$g(t)=\sin (2 \pi t) \cos (3 \pi t)$",linewidth=2)plt.axis([0.0,5.01,-1.0,1.5])plt.xlabel("t")plt.ylabel("v")plt.title("a simple example")plt.grid(True)plt.legend()plt.show()

The drawing result is as follows,

This is just a very simple situation. In other cases, for example, two Y axes, multiple subgraphs, subgraphs, titles, unstandard subgraph la S, and so on, see the subsequent updates.

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.