Home > Backend Development > Python Tutorial > Detailed explanation of Python data visualization tool Matplotlib

Detailed explanation of Python data visualization tool Matplotlib

黄舟
Release: 2017-10-13 11:05:58
Original
2251 people have browsed it

The following editor will bring you an article based on the Python data visualization tool Matplotlib, an introduction to drawing, and a detailed explanation of Pyplot. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

Pyplot

matplotlib.pyplot is a collection of imperative functions, which allows us to use MATLAB like Use matplotlib as well. Each function in pyplot will make corresponding changes to the canvas image, such as creating a canvas, creating a drawing area in the canvas, drawing several lines on the drawing area, adding text descriptions to the image, etc. Below we will appreciate its charm through example code.


import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
Copy after login

The above picture is drawn by the line of code plt.plot([1,2,3,4]) Image, at this time some friends may have a question, "Why is the coordinate range of the X-axis 0-3, and the coordinate range of the Y-axis is 1-4?"

This is because , when we use the plot() command function, if we only pass a value list or array as a parameter to the function, matplotlib will treat this value list as the value of the Y-axis, and then automatically generate it based on the number N of values ​​on the Y-axis. A list of values ​​[0,N-1] as the X-axis value. So the Y-axis value in the above picture is the list we gave [1,2,3,4], and the X-axis value is the automatically generated list [0,1,2,3].

Some friends who see this may think that this is too weak. Don't worry, let's learn step by step. The above picture is just a very simple example. In fact, the plot() command is very powerful. Through this command, we can pass multiple image parameters at the same time. For example, if we want to give the values ​​of the X-axis and Y-axis at the same time, we can achieve it through the following line of code:


##

plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) #X:[1, 2, 3, 4],Y:[1, 4, 9, 16]
Copy after login

In addition, we can also use MATLAB like MATLAB After each set of X-axis and Y-axis values, a string parameter in the form of "color + line type" is passed. This parameter can set the color and type of the line in our image. The default parameter is 'b-', which means Solid blue line. The color characters supported by the

command are:

'b': blue

'g': green
'r': red
'c': cyan
'm': magenta
'y': yellow
'k': black
'w': white

Command support Line characters:

So, when we want to use red dots to display the data in the above code, we can achieve it through the following code:


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

When we have multiple groups of data, we can set the line type and color behind each group:


import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0., 5., 0.2)
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()
Copy after login

The above is the detailed content of Detailed explanation of Python data visualization tool Matplotlib. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template