Getting started with "python" matplotlib.pyplot

Source: Internet
Author: User
Tags cos

Matplotlib.pyplot Introduction

Matplotlib's Pyplot Sub-Library provides a mapping API similar to MATLAB, allowing users to quickly draw 2D charts.
Matplotlib.pyplot is a collection of command-line functions, each of which modifies the image, such as creating a graph, creating a drawing area on the image, drawing lines on the drawing area, labeling on the line, and so on.
Here is a brief introduction to the basic use of Pyplot:

(1) Use plot () function to draw

Plot () is a line-drawing function, the following small example gives ploy () a list of data [1,2,3,4],matplotlib assumes that it is a numerical sequence of the y-axis, and then automatically generates the value of the x-axis, since Python is starting from 0, so here the x-axis sequence corresponds to [ 0,1,2,3].

import matplotlib.pyplot as pltplt.plot([1,2,3,4])plt.ylabel(‘some numbers‘)    #为y轴加注释plt.show()

Plot () can also accept X, y paired parameters, and an optional parameter is the marker and color for the line, the plot function default line is a solid blue line, that is, the string ' B ', you can choose your favorite tags and colors.

import matplotlib.pyplot as pltplt.plot([1,2,3,4], [1,4,9,16], ‘ro‘)plt.axis([0, 6, 0, 20])plt.show()

The axis () function gives a list of shapes such as [Xmin,xmax,ymin,ymax], specifying the range of axes.
This is useful for numerical processing, such as giving a numpy array (arrays), and the following small example gives different lines.

import numpy as npimport matplotlib.pyplot as plt# evenly sampled time at 200ms intervalst = np.arange(0., 5., 0.2)# red dashes, blue squares and green trianglesplt.plot(t, t, ‘r--‘, t, t**2, ‘bs‘, t, t**3, ‘g^‘)plt.show()

(2) Properties of the line

You can set the properties of a line in different ways:

Keywords with parameters

Plt.plot (x, y, linewidth=2.0)
Modify the line width in this way

Set method using the Line2D instance

The plot function returns a list of lines, such as Line1,line2 = Plot (x1,y1,x2,y2).
Since we have only one straight line, for a list of length 1, we can use commas to get the first element of a list

line, = plt.plot(x,y,‘-‘)line.set_antialiased(False) #关闭抗锯齿像素
Using the Pyplot setp () command

You can also use the SETP () command to set up a list or a single object, and to provide a MATLAB-style way to use it

lines = plt.plot(x1, y1, x2, y2)# use keyword argsplt.setp(lines, color=‘r‘, linewidth=2.0)# or MATLAB style string value pairsplt.setp(lines, ‘color‘, ‘r‘, ‘linewidth‘, 2.0)
Properties of the Line2D
(3) Multiple images

pyplot, like Matlab, has the concept of the current image and the current coordinates, and all the commands are set on the current coordinates.
GCA () returns the current coordinate instance (a matplotlib.axes.Axes instance), and GCF () returns the current image (Matplotlib.figure.Figure instance). The
Below is a small example of producing two sub-images.

import numpy as npimport matplotlib.pyplot as pltdef f(t):    return np.exp(-t) * np.cos(2*np.pi*t)t1 = np.arange(0.0, 5.0, 0.1)t2 = np.arange(0.0, 5.0, 0.02)plt.figure(1)plt.subplot(211)plt.plot(t1, f(t1), ‘bo‘, t2, f(t2), ‘k‘)plt.subplot(212)plt.plot(t2, np.cos(2*np.pi*t2), ‘r--‘)plt.show()

The figure () command is optional because figure (1) is created by default.
The subplot () command specifies a coordinate system, the default is subplot (111), the 111 parameter describes the number of rows NumRows, the number of columns Numcols, and the first few images fignum (Fignum ranges from 1 to numrows*numcols).
Subplot (211) specifies that two lines are displayed, one for each row, and the next for the first image.

You can use CLF () to clear the current image and use the CLA () to clear the current coordinates.

(4) text description for the image

text () command can be used to add text anywhere, while Xlabel (), Ylabel (), and title () are used to add text at the specified location. The
All text () command returns a Matplotlib.text.Text instance, or you can set the properties of the text by using either the keyword or the SETP () function.

import numpy as npimport matplotlib.pyplot as pltmu, sigma = 100, 15x = mu + sigma * np.random.randn(10000)# the histogram of the datan, bins, patches = plt.hist(x, 50, normed=1, facecolor=‘g‘, alpha=0.75)plt.xlabel(‘Smarts‘)plt.ylabel(‘Probability‘)plt.title(‘Histogram of IQ‘)plt.text(60, .025, r‘$\mu=100,\ \sigma=15$‘)plt.axis([40, 160, 0, 0.03])plt.grid(True)plt.show()

Reprint please indicate the author Jason Ding and its provenance
GitHub home page (http://jasonding1354.github.io/)
CSDN Blog (http://blog.csdn.net/jasonding1354)
Jane Book homepage (http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)

Getting started with "python" matplotlib.pyplot

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.