The following small series for everyone to bring a python-based data visualization tool matplotlib, Drawing Primer, Pyplot detailed. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
Pyplot
Matplotlib.pyplot is a collection of command functions that allow us to use matplotlib as if using MATLAB. Each function in the Pyplot changes the canvas image, such as creating a canvas, creating a plot area in the canvas, drawing a few lines on the plot area, adding captions to the image, and so on. Let's take a look at his charms by example code.
Import Matplotlib.pyplot as Pltplt.plot ([1,2,3,4]) Plt.ylabel (' Some numbers ') plt.show ()
Is the image we draw through the line of Plt.plot ([1,2,3,4]), when some small partners may have a question, "Why is the axis range of the x axis 0-3, and the axis of the Y axis is 1-4?" ”
This is because, when we use the plot () command function, if we only pass a list of values or arrays as arguments to the function, Matplotlib will take the list of values as the y-axis value, and then automatically generate a numeric list [0,n-1] as the x-axis value based on the number of y-axis values N. So the y-axis value is the list we given [1,2,3,4],x axis values are automatically generated list [0,1,2,3].
It's too weak to see some of the little friends here who might think. Let's not worry, we are learning in a very simple example, but the function of the plot () command is very powerful, we can pass multiple image parameters simultaneously. For example, we want to give the x-axis and y-axis values at the same time, and we can do that with the following line of code:
Plt.plot ([1, 2, 3, 4], [1, 4, 9,]) #X: [1, 2, 3, 4],y:[1, 4, 9, 16]
In addition, we can also like Matlab in each set of x-axis and y-axis values of a string Parameter form "Color + linetype", this parameter can set the color and type of lines in our image, the default parameter is ' B ', which represents the blue solid 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
Line characters supported by the command:
So, when we want to show the data in the above code with a red dot, we can do it with the following code:
Import Matplotlib.pyplot as Pltplt.plot ([1,2,3,4], [1,4,9,16], ' ro ') Plt.axis ([0, 6, 0,]) plt.show ()
When we have multiple sets of data, we can set the line style and color separately 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 ()