Matplotlib, a Python-based data visualization tool, is used to get started with plotting, including Pyplot and matplotlibpyplot.
Pyplot
Matplotlib. pyplot is a set of command functions. It allows us to use matplotlib like MATLAB. Each function in pyplot changes the canvas image accordingly, such as creating a canvas, creating a drawing area in the canvas, drawing several lines in the drawing area, and adding text descriptions to the image. Let's take a look at his charm through the instance code.
import matplotlib.pyplot as pltplt.plot([1,2,3,4])plt.ylabel('some numbers')plt.show()
We use plt. plot ([,]) This line of code draws the image, at this time some friends may have a question, "Why is the X axis range 0-3, what about the axis of the Y axis between 1 and 4?"
This is because, when we use the plot () command function, if we only pass a Value List or array to the function as the parameter, matplotlib regards the Value List as the value of the Y axis, then a value list [0, N-1] is automatically generated as the value of the X axis according to the number N of the Y axis. Therefore, the y-axis value is the given list [,], and the X-axis value is the automatically generated list [,].
Some friends may think that this is too weak. Don't worry. Let's learn it step by step. It's just a simple example. In fact, the plot () command is very powerful. With this command, we can pass multiple image parameters at the same time. For example, if we want to specify the values of the X axis and Y axis at the same time, we can use the following code:
plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) #X:[1, 2, 3, 4],Y:[1, 4, 9, 16]
In addition, we can pass a string parameter in the form of "color + line" after each set of X-axis and Y-axis values like MATLAB, this parameter can set the color and type of the line in our image. The default parameter is 'B-', which indicates the blue solid line.
The command supports the following color characters:
'B': Blue
'G': Green
'R': red
'C': cyan
'M': magenta
'Y': Yellow
'K': Black
'W': white
Line Characters supported by the command:
So when we want to display the data in the above Code with a red dot, we can use the following code:
import matplotlib.pyplot as pltplt.plot([1,2,3,4], [1,4,9,16], 'ro')plt.axis([0, 6, 0, 20])plt.show()
When we have multiple groups of data, we can set the line type and color respectively after each group:
import matplotlib.pyplot as pltimport numpy as npt = np.arange(0., 5., 0.2)plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')plt.show()
The above article is based on the Python data visualization tool Matplotlib. For more information about plotting, see Pyplot. I hope you can give us a reference and support.