python學習筆記(14):視覺化分析
一、Matplotlib
1.用於創建出版質量圖表的繪圖工具庫
2.目的的為Python構建一個Matlab式的繪圖介面
3.import matplotlib.pyplot as plt:pyplot模組包含了常用的matplotlib API函式
4.figure
(1)Matplotlib的影象均位於figure物件中,建立figure:plt.figure()
#引入 matplotlib包 import matplotlib.pyplot as plt %matplotlib inline
#建立figure
fig = plt.figure()
5.Subplot
(1)fig.add_subplot(a,b,c)
a,b表示將fig分割成axb的區域,c表示當前選中要操作的區域,注意編號從1號開始
返回的是AxesSubplot物件
plot繪圖的區域是最後一次指定的subplot的位置
在指定的sublot裡結合scipy繪製統計圖
#在一張圖上做子圖 ax1 = fig.add_subplot(2,2,1) ax2= fig.add_subplot(2,2,2) ax3=fig.add_subplot(2,2,4) ax4 = fig.add_subplot(2,2,4)
#在subplot上來作圖
import numpy as np
random_arr = np.random.randn(100) #生成100個隨機數隨機分佈
#print random_arr
#預設是在最後一次使用subplot的位置上作圖,但是在jupyter裡是無效的
plt.plot(random_arr) #這裡預設作的是線圖
plt.show()
(2)直方圖 hist;s散點圖scatter;柱狀圖 bar;矩陣繪圖plt.imshow()
#在指定的subplot上來繪圖 import scipy as sp from scipy import stats x= np.linspace(-5,15,50) #這裡表示生成一組數,資料的範圍是從-5到15,將資料平均分成50份 print(x.shape) #打印出資料的分佈以及資料的大小 print(x) #繪製高斯分佈,下面的這個x是x軸,sp.stats.norm.pdf是y軸 plt.plot(x,sp.stats.norm.pdf(x=x,loc = 5,scale=2)) #繪製疊加直方圖 sp.stats.norm.rvs是生成一個正太分佈的直方圖,將資料分為50個小組,指定顏色,指定透明度 plt.hist(sp.stats.norm.rvs(loc=5,scale=2,size=200),bins=50,normed=True,color='red',alpha=0.5) plt.show()
#繪製直方圖 plt.hist(np.random.randn(100),bins=10,color='b',alpha=0.3) plt.show()
(3)散點圖scatter
#繪製散點圖,x是一個向量,y也是一個向量 x = np.arange(50) y = x + 5*np.random.rand(50) plt.scatter(x,y)
(4)柱狀圖bar
#柱狀圖,這個圖包括了常規作圖的基本的元素 x= np.arange(5) #要生成5個柱子,就需要生成5個座標 #下面需要繪製兩個柱狀圖,np.random.randint(1,25,size=(2,5))表示生成從1到25的隨機整數 #最後生成的形狀是2行5列的形狀 y1,y2=np.random.randint(1,25,size=(2,5)) print(x) print('y1',y1) #生成的是1到25之間的隨機整數,生成的大小是5列的資料 print(type(y1)) #<class 'numpy.ndarray'> print('y2',y2) width=0.25 #指定柱子的寬度為0.25 ax = plt.subplot(1,1,1) #生成1x1的圖,現在在1的位置上 ax.bar(x,y1,width,color='r') #這個地方先畫第一組 ax.bar(x+width,y2,width,color='g') #因為現在0,1,2,3,4位置被佔據了,現在再在原來的位置上作圖會有疊加,挪動的位置就是前面柱子的寬度 ax.set_xticks(x+width) # 將x軸的標記挪動width寬度 ax.set_xticklabels(['a','b','c','d','e']) #這列設定xlabel的標籤 plt.show() #xticks,yticks #xlable,ylable #x_lim,y_lim #title
(5)矩陣繪圖 plt.imshow():混淆矩陣,三個維度的關係,通過顏色來進行分析
import numpy as np #矩陣繪圖 m = np.random.rand(10,10) print(m) #interpolation='nearest'輸出的時候預設的差值的方法,cmap=plt.cm.ocean設定藍色的主題 #在機器學習中主要用於繪製混淆矩陣,三分類問題,識別貓狗豬。當類比較多的時候,混淆矩陣就會很有用 plt.imshow(m,interpolation='nearest',cmap=plt.cm.ocean) plt.colorbar() plt.show()
(6)繪製子圖
#subplots(2,2s)是指定資料是幾行幾列,這裡繪圖是兩行兩列的子圖 fig,subplot_arr = plt.subplots(2,2) #這裡是指定在0行0列的位置上繪製直方圖,這是一個正常的情況 subplot_arr[0][0].hist(np.random.randn(100),color='b',alpha=0.3) plt.show()
(7)顏色、標記、線型
ax.plot(x,y,'r--'):等價於ax.plot(x,y,linestyle='--',color='r')
顏色:b blue
g:green
r:red
c:cyan
m:magenta
y:yello
k:black
w:white
(8)刻度、標籤和圖例
設定刻度範圍
plt.xlim(),plt.ylim()
ax.set_xlim(),ax.set_ylim()
設定顯示的刻度
plt.xticks(),plt.yticks()
設定刻度標籤
ax.set_xticklabels(),ax.set_yticklabels()
設定座標軸標籤
ax.set_xlabel(),ax.set_ylabel()
設定標題:ax.set_title()
圖例
ax.plot(label='legend')
ax.legend()、plt.legent()
loc='best' 自動選擇放置圖例的最佳位置
fig,ax = plt.subplots(1) ax.plot(np.random.randn(1000).cumsum(),label='line0') #設定刻度 ax.set_xlim([0,800]) #設定顯示的刻度 ax.set_xticks(range(0,500,100)) #設定刻度標籤 ax.set_yticklabels(['Jan','Feb','Mar']) #設定座標軸的標籤 ax.set_xlabel('Number') ax.set_ylabel('Month') #設定標題 ax.set_title('Example') #圖例 ax.plot(np.random.randn(1000).cumsum(),label='學校') ax.plot(np.random.randn(1000).cumsum(),label='line2') ax.legend() ax.legend(loc='best') plt.legend()
(9)matplotlib設定
plt.rc()