The Python matplotlib library Getting Started Guide

Source: Internet
Author: User

This article mainly introduces Python Matplotlib library Getting Started Guide, this article explains what Matplotlib is, then gives the Matplotlib basic drawing instance such as draws the line chart, draws the multi-line graph, and has given the legend function use example, needs the friend to be possible to refer to under

Matplotlib Introduction

Matplotlib is a Python toolbox for data visualization for scientific calculations. With it, Python can draw a variety of data graphics, such as MATLAB and Octave. Originally imitate the MATLAB graphics command, but with MATLAB is independent of each other.

Can quickly draw 2D charts with simple interfaces in Matplotlib

Preliminary matplotlib

The Pyplot library in Matplotlib provides a drawing API similar to MATLAB.

The code is as follows:

Import Matplotlib.pyplot as Plt #导入pyplot子库

Plt.figure (figsize= (8, 4)) #创建一个绘图对象 and sets the width and height of the object, and if you do not create a direct call to plot, Matplotlib creates a drawing object directly

Plt.plot ([1, 2, 3, 4]) #此处设置y的坐标为 [1, 2, 3, 4], the coordinates of x default to [0, 1, 2, 3] draw in the drawing object, you can set the label, color, and linewidth keyword parameters

Plt.ylabel (' Some numbers ') #给y轴添加标签, label the x-axis xlable

Plt.title ("Hello"); #给2D图加标题

Plt.show () #显示2D图

Basic drawing

Draw a line chart

Related to the coordinates of the selected points

Copy code code as follows:

#-*-Coding:utf-8-*-

#!/usr/bin/env python

Import NumPy as NP

Import Matplotlib.pyplot as Plt

x = [0, 1, 2, 4, 5, 6]

y = [1, 2, 3, 2, 4, 1]

Plt.plot (x, y, '-*r ') # dashed, star dot, red

Plt.xlabel ("X-axis")

Plt.ylabel ("Y-axis")

Plt.show ()

Change the style of a line view plot function parameter settings

Multi-line diagram

You can draw more than one X-y coordinate in the plot function.

The code is as follows:

#-*-Coding:utf-8-*-

#!/usr/bin/env python

Import NumPy as NP

Import Matplotlib.pyplot as Plt

x = [0, 1, 2, 4, 5, 6]

y = [1, 2, 3, 2, 4, 1]

z = [1, 2, 3, 4, 5, 6]

Plt.plot (x, y, '--*r ', X, Z, '-.+g ')

Plt.xlabel ("X-axis")

Plt.ylabel ("Y-axis")

Plt.title ("Hello World")

Plt.show ()

Columnar chart

The code is as follows:

#-*-Coding:utf-8-*-

#!/usr/bin/env python

Import NumPy as NP

Import Matplotlib.pyplot as Plt

x = [0, 1, 2, 4, 5, 6]

y = [1, 2, 3, 2, 4, 1]

z = [1, 2, 3, 4, 5, 6]

Plt.bar (x, y)

Plt.xlabel ("X-axis")

Plt.ylabel ("Y-axis")

Plt.show ()

Child graphs

The subplot () function indicates the number of numrows rows, numcols columns, and fignum graphs. The number of graphs cannot exceed the number of rows and columns

The code is as follows:

#-*-Coding:utf-8-*-

#!/usr/bin/env python

Import NumPy as NP

Import Matplotlib.pyplot as Plt

x = [0, 1, 2, 4, 5, 6]

y = [1, 2, 3, 2, 4, 1]

z = [1, 2, 3, 4, 5, 6]

Plt.figure (1)

Plt.subplot (211)

Plt.plot (x, y, '-+b ')

Plt.subplot (212)

Plt.plot (x, Z, '-.*r ')

Plt.show ()

Text add

Use the text () function, as well as Xlabel (), Ylabel (), title () function when you need to price the text on the picture.

The text () function returns Matplotlib.text.Text, which explains the function in detail

The code is as follows:

#-*-Coding:utf-8-*-

#!/usr/bin/env python

Import NumPy as NP

Import Matplotlib.pyplot as Plt

x = [0, 1, 2, 4, 5, 6]

y = [1, 2, 3, 2, 4, 1]

Plt.plot (x, y, '-.*r ')

Plt.text (1, 2, "I ' m A Text")//The first two parameters represent text coordinates, and the third parameter is the text to be added

Plt.show ()

Introduction to the Legend

The legend () function implements the legend feature, he has two parameters, the first is a style object, and the second is a descriptive character

The code is as follows:

#-*-Coding:utf-8-*-

#!/usr/bin/env python

Import NumPy as NP

Import Matplotlib.pyplot as Plt

LINE_UP, = Plt.plot ([1,2,3], label= ' Line 2 ')

Line_down, = Plt.plot ([3,2,1], label= ' line 1 ')

Plt.legend (HANDLES=[LINE_UP, Line_down])

Plt.show ()

or call Set_label () to add a legend

Copy code code as follows:

#-*-Coding:utf-8-*-

#!/usr/bin/env python

Import NumPy as NP

Import Matplotlib.pyplot as Plt

Line, = Plt.plot ([1, 2, 3])

Line.set_label ("label Via Method")

Plt.legend ()

Plt.show ()

Add a legend to multiple bars at the same time

The code is as follows:

#-*-Coding:utf-8-*-

#!/usr/bin/env python

Import NumPy as NP

Import Matplotlib.pyplot as Plt

Line1, = Plt.plot ([1, 2, 3])

Line2, = Plt.plot ([3, 2, 1], '--b ')

Plt.legend ((line1, line2), (' line1 ', ' line2 ')

Plt.show ()

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.