This article mainly introduces the use of Python to draw a summary of the chart, small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.
Before using Python to draw a chart, we need to install two library files NumPy and matplotlib first.
NumPy is a python open-source numerical extension that can be used to store and manipulate large matrices, which is more efficient than Python's own data structures; Matplotlib is a python image frame that uses its drawing effect to resemble a graphic drawn under Matlab.
Below I'll show you how to use Python drawing with some simple code.
First, graphic drawing
Histogram
Importmatplotlib.pyplotaspltimportnumpyasnpmu=100sigma=20x=mu+sigma*np.random.randn (20000) # Number of samples plt.hist (x,bins =100,color= ' green ', normed=true) # Bins shows a few straight sides, normed whether to standardize the data plt.show ()
Bar chart
Importmatplotlib.pyplotaspltimportnumpyasnpy=[20,10,30,25,15]index=np.arange (5) Plt.bar (Left=index,height=y, Color= ' green ', width=0.5) plt.show ()
Line chart
Importmatplotlib.pyplotaspltimportnumpyasnpx=np.linspace ( -10,10,100) y=x**3plt.plot (x,y,linestyle= '--', color= ' Green ', marker= ' < ') plt.show ()
Scatter chart
Importmatplotlib.pyplotaspltimportnumpyasnpx=np.random.randn (Y=X+NP.RANDOM.RANDN) *0.5plt.scatter (x, y , s=5,marker= ' < ') # s represents area, marker represents graphics Plt.show ()
Pie chart
Importmatplotlib.pyplotaspltimportnumpyasnplabels= ' A ', ' B ', ' C ', ' D ' fracs=[15,30,45,10]plt.axes (aspect=1) #使x The y-axis scale is the same explode=[0,0.05,0,0]# highlights a part of the area plt.pie (x=fracs,labels=labels,autopct= '%.0f%% ', Explode=explode) # autopct Display percent plt.show ()
Box-shaped diagram
Mainly used to show the dispersion of data. The graph is divided into top edge, top four, median, bottom four, bottom edge. Outliers at the point outside
Importmatplotlib.pyplotaspltimportnumpyasnpnp.random.seed (Data=np.random.normal) (size= (1000,4), loc=0,scale= 1) labels=[' A ', ' B ', ' C ', ' D ']plt.boxplot (data,labels=labels) plt.show ()
Second, the image adjustment
1, 23 kinds of point shape
"." Point "," Pixel "O" circle "V" triangle_down "^" Triangle_up "<" Triangle_left ">" triangle_right "1" tri_down "2" tri_ Up "3" Tri_left "4" tri_right "8" octagon "s" Square "P" Pentagon "*" star "H" Hexagon1 "H" hexagon2 "+" plus "x" x "D" Diamond "D" Thin_diamond
2, 8 types of internal default color abbreviations
B:blueg:greenr:redc:cyanm:magentay:yellowk:blackw:white
3, 4 kinds of linear
-Solid line-dashed-dash-dot line
4. Draw a sub-chart on a picture
Importmatplotlib.pyplotaspltimportnumpyasnpx=np.arange (1,100) plt.subplot (221) #2行2列第1个图plt. Plot (x,x) Plt.subplot (222) Plt.plot (x,-x) plt.subplot (223) plt.plot (x,x*x) plt.subplot (224) Plt.plot (X,np.log (x)) Plt.show ()
5. Generate Mesh
Importmatplotlib.pyplotaspltimportnumpyasnpy=np.arange (1,5) plt.plot (y,y*2) Plt.grid (True,color= ' g ', linestyle= '- -', linewidth= ' 1 ') plt.show ()
6. Generate legend
Importmatplotlib.pyplotaspltimportnumpyasnpx=np.arange (1,11,1) plt.plot (x,x*2) plt.plot (x,x*3) plt.plot (x,x*4) Plt.legend ([' Normal ', ' Fast ', ' Faster ']) plt.show ()
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.