Brief introduction
Matplotlib is a library of professional drawing tools in Python, and if you want to draw beautiful graphics from Python, it must be.
Ready to install NumPy and matplotlib libraries before you start drawing, of course, Python Basic library is essential, NumPy is a professional array, matrix processing tool.
? Python
? Numpy -This is the module which does most array and mathematical manip-
Ulation
? matplotlib - This is the module you'll be a using for plotting
You can check these is installed by going to a terminal and typing:
Import Target Library:
$ python>>> Import numpy as np>>> import Pylab as pl
Basic Drawing line or curve
Next, let's try the basic drawing:
# Lineplot.pyimport NumPy as Npimport Pylab as pl# make an array of X Valuesx = [1, 2, 3, 4, 5]# make a array of y values for each x Valuey = [1, 4, 9, +, 25]# use Pylab to plot x and ypl.pl OT (x, y) # show the plot on the Screenpl.show () ****************************************************
Scatter plot scatter plot
# Scatterplot.pyimport NumPy as Npimport Pylab as pl# make an array o f x valuesx = [1, 2, 3, 4, 5]# make a array of y values for each x Valuey = [1, 4, 9,, 25]# use Pylab to plot x and Y As Red Circlespl.plot (x, y, ' ro ') # show the plot on the Screenpl.show () ************************************************* ***
Basic properties of graphs
When there are too many elements in the graph, you can change the color, size, and shape of the dots to differentiate the elements in the diagram, Matplot can set the following basic colors:
Pl.plot (x, Y, ' R ')
This will make a red line.
What if you want to change the shape of the line? Lines with the following patterns:
['-' | '--' | '-. ' | ': ' | ' None ' | "' | "' ] |
Plot (x, Y, '--')
The underline is a dashed line.
And can also change the shape of marker:
If I use the following code, I draw a blue star marker:
Plot (x, y, ' b* ')
Label drawing of course have to have a mark, you can use the Xlabel and Ylabel method to mark the graph separately:
Pl.xlabel (' Put text here ') Pl.ylabel (' Put text here ')
Of course not missing the title:
Pl.title (' Put plot title here ')
If we find that the proportions of the graphs are not appropriate, then we can adjust the coordinate range of the coordinates of the graph:
Pl.xlim (X_low, X_high) Pl.ylim (Y_low, Y_high)
The following is an example:
#lineplotAxis. Pyimport NumPy as Npimport Pylab as pl# make an array of x Valuesx = [1, 2, 3, 4, 5]# make a array of y values for each x Valuey = [1, 4, 9,, 25]# use Pylab to plot x and Y Pl.plot (x, y) # give plot a titlepl.title (' Plot of y vs. X ') # Make Axis Labelspl.xlabel (' x axis ') Pl.ylabel (' Y axis ') # set a XIs Limitspl.xlim (0.0, 7.0) Pl.ylim (0.0, 30.) # show the plot on the Screenpl.show () ****************************************************
Multiple graphs draw multiple graphs on a single graph, you can use the plot method continuously, note that in order to facilitate the separation of graphics, you can use different colors:
Plot (x1, y1, ' R ') plot (x2, y2, ' G ')
Figure LegendsThe legend syntax for Matplot is as follows:
Pl.legend ((PLOT1, Plot2), (' Label1, Label2 '), ' best ', Numpoints=1)
The first parameter is the graphic we need to mark, if there are multiple graphics on the screen, then you can enclose it in parentheses, the second parameter is a specific label, that is, we want to explain to the person looking at the figure what the graph represents, and the third parameter represents where we want to put the graph, Best means that the system is automatically placed in the optimal location and can, of course, be customized to' upper right ', ' upper left ', ' center ', ' lower left ', ' lower right '.
The following is an example diagram:
# Lineplotfiglegend.pyimport NumPy as Npimport Pylab as pl# make X, Y arrays for each graphx1 = [1, 2, 3, 4, 5]y1 = [1, 4, 9,, 25]x2 = [1, 2, 4, 6, 8]y2 = [2, 4, 8,, 16]# use Pylab to Plot x and y:give your plots namesplot1 = Pl.plot (x1, y1, ' r ') Plot2 = Pl.plot (x2, y2, ' Go ') # Give plot a titlepl.title (' Plot of y vs. X ') # Make Axis Labelspl.xlabel (' x axis ') Pl.ylabel (' Y axis ') # Set Axis Limitspl.xlim (0.0, 9.0) Pl.ylim (0.0, 30 .) # Make Legendpl.legend ([Plot1, Plot2], (' Red Line ', ' green circles '), "best", Numpoints=1) # show the plot on the SCREENPL. Show () ****************************************************
Histogram
Here is an example of a histogram:
# Histplot.pyimport NumPy as Npimport Pylab as pl# make an array of RA Ndom numbers with a Gaussian distribution with# mean = 5.0# RMS = 3.0# number of points = 1000data = Np.random.normal (5.0, 3.0, +) # Make a histogram of the data arraypl.hist (data) # Make plot labelspl.xlabel (' Data ') pl.show () ****************** **********************************
Here we first use the mean value of 5, the variance of 3 Gaussian distribution generated 1000 points, and then the point to do the histogram, the left is the default format, the right is the following statement:
Pl.hist (data, histtype= ' stepfilled ')
Of course, you can also manually set the size of the bin in the histogram:
Bins = Np.arange ( -5., 16., 1.) Pl.hist (data, bins, histtype= ' stepfilled ')
A single canvas with multiple images
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python matplotlib Basics