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() # 在一個新窗口中顯示統計圖,
# @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()
# @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()
# @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()