Python matplotlib Library Getting Started Guide

Source: Internet
Author: User
matplotlib Introduction

Matplotlib is a Python toolbox for data visualization of scientific calculations. With it, Python can draw a wide variety of data graphics, such as MATLAB and Octave. It was originally modeled after MATLAB graphics commands, but it was independent of MATLAB.
Quickly draw 2D charts with simple interfaces in the Matplotlib

Preliminary matplotlib

The Pyplot Sub-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] drawing in the drawing object, you can set the label, color, and linewidth keyword parameters
Plt.ylabel (' Some numbers ') #给y轴添加标签, label the x-axis with xlable
Plt.title ("Hello"); #给2D图加标题
Plt.show () #显示2D图

Basic drawing

Draw a line chart

Related to the coordinates of the selected point

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 ') # dotted line, 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 multiple lines by passing in multiple pairs of x-y coordinates 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 ()

Bar 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 ()

Sub-chart

The subplot () function indicates the number of numrows rows, the number of numcols columns, and the number of fignum graphs. The number of graphs cannot exceed the product of 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

You need to use the text () function when you need to price-xlabel the image, as well as the (), Ylabel (), title () function

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")//first two parameters represent text coordinates, and the third parameter is the text to be added
Plt.show ()

Introduction to Legends
The legend () function implements the Legend function, he has two parameters, the first is a style object, 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 ([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

The code is 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 ()


For more legend settings, refer to the official legend tutorial
  • 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.