可視化是在整個數據挖掘的關鍵輔助工具,能夠清晰的理解數據,從而調整咱們的分析方法。python
1. Matplotlib 基本概念express
Matplotlib是python的一個數據可視化工具庫,專門用於開發2D圖表(包括3D圖表), 操做簡單。dom
2. Matplotlib三層結構ide
容器層函數
容器層由Canvas、Figure、Axes三部分組成。工具
Canvas位於最底層的系統層,充當畫板,即放置Figure的工具。spa
Figure是Canvas上方的第一層,也是須要用戶來操做的應用層的第一層,在繪圖的過程當中充當畫布的角色。3d
Axes是應用層的第二層,在繪圖的過程當中至關於畫布上的繪圖區的角色。對象
Figure:指整個圖形(能夠經過plt.figure()設置畫布的大小和分辨率等)圖片
Axes(座標系):數據的繪圖區域
Axis(座標軸):座標系中的一條軸,包含大小限制、刻度和刻度標籤
特色爲:
一個figure(畫布)能夠包含多個axes(座標系/繪圖區),可是一個axes只能屬於一個figure。
一個axes(座標系/繪圖區)能夠包含多個axis(座標軸),包含兩個即爲2d座標系,3個即爲3d座標
輔助顯示層
輔助顯示層爲Axes(繪圖區)內的除了根據數據繪製出的圖像之外的內容,主要包括Axes外觀(facecolor)、邊框線(spines)、座標軸(axis)、座標軸名稱(axis label)、座標軸刻度(tick)、座標軸刻度標籤(tick label)、網格線(grid)、圖例(legend)、標題(title)等內容。
圖像層
圖像層指Axes內經過plot、scatter、bar、histogram、pie等函數根據數據繪製出的圖像
總結
Canvas(畫板)位於最底層,用戶通常接觸不到;
Figure(畫布)創建在Canvas之上;
Axes(繪圖區)創建在Figure之上;
座標軸(axis)、圖例(legend)等輔助顯示層以及圖像層都是創建在Axes之上。
3.plt的基本用法
3.1 Figure對象
matplotlib的圖像都位於Figure對象中,咱們能夠調用plt.figure()來建立Figure對象。
fig = plt.figure()
figure有一個比較重要的參數figsize,它衡量圖片的大小和縱橫比(單位爲inch):
fig = plt.figure(figsize=(4,5))
好比,以上代碼表明創建一個寬度爲4inch,高度爲5inch的figure對象。
3.2 plot的使用
有了figure對象以後,就能夠利用plot函數做圖了。注意不可使用figure對象來調用plot,按照慣例咱們使用plt.plot()來做圖,而圖像自動分配到上一個創建的figure中。
3.3 如何在同一個figure內部設置多個圖片
figure對象調用add_subplot函數來添加figure內部不一樣位置的圖片,add_subplot函數的3個參數分別爲figure內部縱向和橫向的字圖片個數,以及當前建立的子圖片是第幾個,例如:無錫看婦科的醫院 http://www.ytsgfk120.com/
fig = plt.figure()
# add_subplot返回的是一個subplot對象
sp1 = fig.add_subplot(2,3,1)
sp2 = fig.add_subplot(2,3,2)
sp3 = fig.add_subplot(2,3,3)
sp4 = fig.add_subplot(2,3,4)
fig
若是要在subplot內部做圖,咱們只須要用對應的subplot對象調用plot便可:
sp1.plot(np.random.randn(50), 'k--', color='r')
fig
3.4 如何調整subplot的間距
有時候各subplot的間距會過大或者太小,這時候與咱們須要使用subplots_adjust函數來調整間距:
fig.tight_layout() # 調整總體空白
plt.subplots_adjust(wspace =0, hspace =0) # 調整子圖間距
plt.subplots_adjust(left=None, bottom=None, right=None, top=None,wspace=None,hspace=None)
參數詳解:
left = 0.125 # the left side of the subplots of the figure
right = 0.9 # the right side of the subplots of the figure
bottom = 0.1 # the bottom of the subplots of the figure
top = 0.9 # the top of the subplots of the figure
wspace = 0.2 # the amount of width reserved for blank space between subplots,
# expressed as a fraction of the average axis width
hspace = 0.2 # the amount of height reserved for white space between subplots,
# expressed as a fraction of the average axis height
# 調整fig內部的subplot長寬間距都爲0.5
fig.subplots_adjust(wspace = 0.5, hspace = 0.5)
fig