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.
Before using Python to draw a chart, we need to install two library files NumPy and matplotlib
Pip Install Numpypip Install Matplotlib
Generate histograms
import NumPy as NP from pylab num =100sigma =20x =num+sigma*np.random.randn (20000) #样本数量 plt.hist (x,bins =100,color= " green " , Normed=true) #bins显示有几个直方, normed whether the data is normalized plt.show () #显示图像
Plt.savefig () #保存图片
Operation Result:
generate a bar chart
Import NumPy as NP from Import *value=[22,13,34]index=["root"," Admin","lyshark"]
#index =np.arange (5) Plt.bar (left =index,height=value,color="green", width= 0.5) plt.show ()
Operation Result:
generate a line chart
Import NumPy as NP from Import *x=np.linspace ( -10,10,100) y=x**3plt.plot (x,y,linestyle=" -- ", color="green", marker="< " ) plt.show ()
Operation Result:
Generating Scatter plots
Import NumPy as NP from Import *x=np.random.randn (+) y=x+np.random.randn (+) *0.5plt.scatter (x,y,s =5,marker="<") #s represents the area marker represents the graph Plt.show ()
Operation Result:
Generate pie chart
ImportNumPy as NP fromPylabImport*Labels="Cangjingkong","Jizemingbu","Boduoyejieyi","Xiaozemaliya"Fracs=[45,10,30,15]plt.axes (Aspect=1) Explode=[0,0.05, 0,0]plt.pie (x=fracs,labels=labels,autopct="%0f%%", explode=explode) plt.show ()
Operation Result:
Create a Box chart
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
Import NumPy as NP from Import *np.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 ()
Operation Result:
Generate multiple Legends
Import NumPy as NP from Import *x=np.arange (1,11,1) plt.plot (x,x) plt.plot (x,x) Plt.plot (x,x) plt.legend (["boduoyejieyi"," cangjingkong","jiatengying"]) plt.show ( )
Operation Result:
python-realization of Chart drawing summary