1. Draw according to coordinate points:
ImportNumPy as NPImportMatplotlib.pyplot as Pltx= Np.array ([1,2,3,4,5,6,7,8]) y= Np.array ([3,5,7,6,2,6,10,15]) Plt.plot (x, Y,'R')#polyline 1 x 2 y 3 colorPlt.plot (x, Y,'g', lw=10)#4 Line W#polyline pie-like columnx = Np.array ([1,2,3,4,5,6,7,8]) y= Np.array ([13,25,17,36,21,16,10,15]) Plt.bar (x, Y,0.2,alpha=1,color='b')#5 Color 4 Transparency 3 0.9Plt.show ()
Import Matplotlib.pyplot as Plt Import NumPy as NP # 66 numbers are collected from 1-----1. That is to say, the drawing is a 66-point connection . # Note: If the points are too small it will cause the picture to be drawn two times the function image is not smoothed x = Np.linspace ( -1, 1,66)# Draws the image of the y=2x+1 function y = 2 * x + 1< C11>plt.plot (x, y) plt.show ()# Draws the image of the x^2 function y = x**2plt.plot (x, y) plt.show ()
Use of Figure:
"""use of figure"""ImportMatplotlib.pyplot as PltImportNumPy as NP#x = Np.linspace (-1, 1, 50)#Figure 1Y1 = 2 * x + 1plt.figure () plt.plot (x, y1)#Figure 2y2 = x**2plt.figure () plt.plot (x, y2)#Figure 3, specifying the number of the figure and specifying the size of the figure, specifying the color, width, and type of the line#two drawings on an axisy2 = x**2plt.figure (Num= 5, Figsize = (4, 4) ) Plt.plot (x, y1) plt.plot (x, y2, color='Red', LineWidth = 1.0, LineStyle ='--') plt.show ()
There will be a total of three drawings, the first two of which are the same as those drawn from the simple case above. The third one:
To set an axis:
ImportMatplotlib.pyplot as PltImportNumPy as NP#draw a normal imagex = Np.linspace (-1, 1, 50) Y1= 2 * x + 1y2= X**2plt.figure () plt.plot (x, y1) plt.plot (x, y2, color='Red', LineWidth = 1.0, LineStyle ='--')#set the range of values for an axisPlt.xlim (-1, 1)) Plt.ylim (0,3))#set the lable of an axis#tags must be added to the font variables: fontproperties= ' Simhei ', fontsize=14. Otherwise it could be garbled .Plt.xlabel (U'This is the x-axis .', fontproperties='Simhei', fontsize=14) Plt.ylabel (U'This is the y-axis .', fontproperties='Simhei', fontsize=14)#set the x-axis scale before 0.25, modified to 0.5#that is, to take 5 points on the axis, the x-axis range is 1 to 1, so the scale becomes 0.5 after 5 points are taken.Plt.xticks (Np.linspace (-1, 1, 5) ) plt.show ()
#gets the current axis, GCA = Get now AxisAx =PLT.GCA ()#set the right and top bordersax.spines[' Right'].set_color ('None') ax.spines['Top'].set_color ('None')#set the x axis to the bottom borderAx.xaxis.set_ticks_position ('Bottom')#set the Y axis to the left borderAx.yaxis.set_ticks_position (' Left')#set X-axis, Y-week at (0, 0) positionax.spines['Bottom'].set_position (('Data', 0)) ax.spines[' Left'].set_position (('Data', 0))
To plot a scatter plot:
ImportNumPy as NPImportMatplotlib.pyplot as Plt#Number of datan = 1024#A random number with a mean of 0 and a variance of 1x = Np.random.normal (0, 1, N) y= Np.random.normal (0, 1, N)#Calculate color Valuescolor =np.arctan2 (y, x)#Plot scatter plotsPlt.scatter (x, y, s =-, c = color, alpha = 0.5)#Set axis rangePlt.xlim (-1.5, 1.5)) Plt.ylim (-1.5, 1.5))#the value of the axis is not displayedPlt.xticks (()) Plt.yticks (()) plt.show ()
To draw a bar chart:
ImportMatplotlib.pyplot as PltImportNumPy as NP#Number of datan = 10x=np.arange (n)#generate data, evenly distributed between (0.5, 1.0)Y1 = (1-x/float (n)) * Np.random.uniform (0.5, 1.0, N) y2= (1-x/float (n)) * Np.random.uniform (0.5, 1.0, N)#draw a histogram, upPlt.bar (x, y1, Facecolor ='Blue', Edgecolor =' White')#draw a histogram, downPlt.bar (x,-y2, Facecolor ='Green', Edgecolor =' White') Temp=zip (x, y2)#display specific values on the bar chart, ha horizontal alignment, VA vertical Alignment forX, yinchzip (x, y1): Plt.text (x+ 0.05, Y + 0.1,'%.2f'% y, ha ='Center', VA ='Bottom') forX, yinchTemp:plt.text (x+ 0.05,-y-0.1,'%.2f'% y, ha ='Center', VA ='Bottom')#Set axis rangePlt.xlim (-1, N) plt.ylim (-1.5, 1.5)#Remove AxesPlt.xticks (()) Plt.yticks (()) plt.show ()
Matplotlib Common operations