1.條形圖
import matplotlib.pyplot as plt plt.style.use('ggplot') # 使用ggplot樣式來模擬ggplot2風格的圖形,ggplot2是一個常用的R語言繪圖包 customers = ['ABC','DEF','GHI','JKL','MNO'] customers_index = range(len(customers)) sale_amounts = [127,90,201,111,232] fig = plt.figure() # 創建基礎圖 ax1 = fig.add_subplot(1,1,1) # 向基礎圖中添加一個子圖 1,1,1表示穿件1行1列的子圖,並使用第一個也是唯一的一個子圖 ax1.bar(customers_index,sale_amounts,align='center',color='darkblue') # 創建條形圖,customers_index設置天性左側在x軸上的坐標,sale_amounts設置條形圖的高度,align 設置條形圖與標簽中間對齊,color設置條形圖的衍射 plt.xlabel('用戶姓名', fontproperties="SimHei") # 設置X軸的標題 plt.ylabel('銷售數量', fontproperties="SimHei") # 設置Y軸的標題 plt.title('銷售額/客戶', fontproperties="SimHei") # 設置title的標題 plt.savefig('bar_plot.png',dpi=400,bbox_inches='tight') # 講統計圖保存在當前文件夾中,文件名為bar_plot.png,dpi=400設置圖形分辨率,【每英寸(1英寸=2.54厘米)的點數】,bbox_inches='tight' 表示在保存圖形時,將圖形四周的空白部分去掉 plt.show() # 在一個新窗口中顯示統計圖,
2.直方圖
# @author: erlang import numpy as np import matplotlib.pyplot as plt plt.style.use('ggplot') mu1,mu2,sigma = 100,130,15 x1 = mu1 + sigma * np.random.randn(10000) # 生成兩個正太分布變量x1和x2,x1的均值是100,x2的均值是130, x2 = mu2 + sigma * np.random.randn(10000) fig = plt.figure() ax1 = fig.add_subplot(1,1,1) n, bins, patches = ax1.hist(x1,bins=50,normed=False,color='darkgreen') #創建兩個柱形圖或稱頻率分布圖,bins= 50表示每個變量的值應該被分成50份 n, bins, patches = ax1.hist(x2,bins=50,normed=False,color='orange',alpha=0.2) # normed= False表示直方圖顯示是平率分布,而不是概率密度。alpha=0.2 表示第二個直方圖應該是透明的 ax1.xaxis.set_ticks_position('bottom') ax1.yaxis.set_ticks_position('left') plt.xlabel('Bins') plt.ylabel('Number中值數',fontproperties="SimHei") fig.suptitle('直方圖',fontproperties="SimHei") ax1.set_title('兩個頻率分布',fontproperties="SimHei") plt.savefig('histogram.png',dpi=400,bbox_inches='tight') plt.show()
3.折線圖
# @author: erlang from numpy.random import randn import matplotlib.pyplot as plt import matplotlib # 保證圖中的中文可以正常顯示 matplotlib.rcParams['font.sans-serif'] = ['SimHei'] matplotlib.rcParams['axes.unicode_minus'] = False plt.style.use('ggplot') plot_data1 = randn(50).cumsum() plot_data2 = randn(50).cumsum() plot_data3 = randn(50).cumsum() plot_data4 = randn(50).cumsum() fig = plt.figure() ax1 = fig.add_subplot(1,1,1) # 穿件4條折線,每條折線都可以通過選項進行設置。使用不同的數據點類型、顏色和現行,label參數盤整折線在圖列中可以正確標記 ax1.plot(plot_data1,marker='o',color=u'blue',linestyle='-',label='Blue solid(藍色固體)',) ax1.plot(plot_data2,marker='+',color=u'red',linestyle='--',label='Red Dashed(紅色虛線)',) ax1.plot(plot_data3,marker='*',color=u'green',linestyle='-.',label='Green Dash Dot(綠色沖點)',) ax1.plot(plot_data4,marker='s',color=u'orange',linestyle=':',label='Orange Dotted(橙色的虛線)',) ax1.xaxis.set_ticks_position('bottom') ax1.yaxis.set_ticks_position('left') ax1.set_title('Line Plots:Markers,colors,and Linestyles(情節:標記、顏色和線型)') plt.xlabel('Draw(畫)') # x軸標題 plt.ylabel('Random Number(隨機數)')# Y軸標題 plt.legend(loc='best') # 指示matplotlib根據圖中空白部分將圖列放在最合適的位置 plt.savefig('line_plot.png',dpi=400,bbox_inches='tight') plt.show()
4散點圖
# @author: erlang
import numpy as np
import matplotlib.pyplot as plt import matplotlib # 保證圖中的中文可以正常顯示 matplotlib.rcParams['font.sans-serif'] = ['SimHei'] matplotlib.rcParams['axes.unicode_minus'] = False plt.style.use('ggplot') x = np.arange(start=1.,stop=15.,step=1.) y_linear = x + 5. * np.random.randn(14) y_quadratic = x** 2 + 10. * np.random.randn(14) # 通過隨機數是數據與一條直線和一條二次曲線悄悄偏離 # 使用numpy的polyfit函數通過函數兩組數據點(x,y_linear)和(x,y_quadratic)擬合出一條直線和一條二次曲線 # 再使用polyid函數根據直線和二次曲線的參數與生成一個線性方程和二次方程 fn_linear = np.poly1d(np.polyfit(x,y_linear,deg=1)) fn_quadratic = np.poly1d(np.polyfit(x,y_quadratic,deg=2)) # fig = plt.figure() ax1 = fig.add_subplot(1,1,1) # 代碼創建帶有兩個回歸曲線的散點圖,'bo'表示(x,_y_linear)點事是藍色圓圈,'go'表示(,x,y_quadratic)點是綠色圓圈, # 同樣'b-'表示(x,y_linear)點之間的顯示一條藍色實線 'g-'表示(,x,y_quadratic)點是綠色實線, 通過linewidth可以設置線的高度 ax1.plot(x,y_linear,'bo',x,y_quadratic,'go',x,fn_linear(x),'b-',x,fn_quadratic(x),'g-',linewidth = 2.) ax1.xaxis.set_ticks_position('bottom') ax1.yaxis.set_ticks_position('left') ax1.set_title('Scatter ploys Regression Lines(散射伎倆回歸直線)',) plt.xlabel('x') plt.ylabel('f(x)') # 設置了x軸和Y軸的范圍。這兩條曲線使用min和max函數基於實際數據設置坐標軸范圍,你也可以使用具體的數值設置范圍,列入xlim(0,20)和ylom(0,200) # 如果你沒有設置坐標范圍,那么matplotlib會替你自己設置, plt.xlim(min(x)-1,max(x)+1) plt.ylim(min(y_quadratic)-10.,max(y_quadratic)+10.) plt.savefig('scatter_plot.png',dpi=400,bbox_inches='tight') plt.show()