Python Matplotlib library Getting Started Guide
This article mainly introduces the Python Matplotlib library Getting Started Guide, this article explains what Matplotlib is, and then provides the Matplotlib Basic Drawing implementation, such as drawing a line chart, drawing a multi-line chart, the example shows how to use the legend function. For more information, see
Introduction to Matplotlib
Matplotlib is a Python toolbox for data visualization in scientific computing. With it, Python can plot a variety of data graphics, such as Matlab and Ave ave. At first, it imitated the Matlab graphical commands, but it was independent of Matlab.
You can use the simple interface in Matplotlib to quickly draw 2D charts.
Matplotlib
The pyplot sub-library in Matplotlib provides plotting APIs similar to matlab.
The Code is as follows:
Import matplotlib. pyplot as plt # import the pyplot sub-database
Plt. figure (figsize = (8, 4) # create a Drawing Object and set the width and height of the object. If you do not create a Drawing Object and call plot directly, Matplotlib will directly create a drawing object.
Plt. plot ([1, 2, 3, 4]) # If the coordinates of y are set to [1, 2, 3, 4], the coordinates of x are [0, 1, 2, 3] drawing in the drawing object. You can set the label, color, and linewidth keyword parameters.
Plt. ylabel ('some numbers ') # Add labels to the Y axis and Add labels to the X axis using xlable
Plt. title ("hello"); # Add a title to the 2D graph
Plt. show () # display 2D images
Basic Drawing
Draw a line chart
Related to the coordinates of the selected point
Copy the 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') # dotted line, Star, red
Plt. xlabel ("x-axis ")
Plt. ylabel ("y-axis ")
Plt. show ()
Change the line style to view the plot function parameter settings
Multi-line chart
You only need to input multiple x-y coordinate pairs in the plot function to draw multiple lines.
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 ()
Subgraph
The subplot () function specifies the number of numrows, the number of numcols columns, and the number of fignum graphs. The number of graphs cannot exceed the number of rows and the number of 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 ()
Add text
You need to use the text () function to adjust the price of text on the image, as well as the xlabel (), ylabel (), title () functions.
The text () function returns matplotlib. text. Text.
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 the text coordinate, and the third parameter is the text to be added
Plt. show ()
Legend Introduction
The legend () function implements the legend function. It has two parameters: the first is the style object, and the second is the description 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 the 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 entries 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 ()