Matplolib是最經常使用的Python數據繪圖工具庫,支持不一樣樣式數據的圖像繪製。本文介紹一些使用Matplotlib庫的注意事項,主要包括:html
其餘:python
subplots()是繪圖的開始,建立不一樣子圖的排列結構。git
以下:github
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=2)
plt.show()
複製代碼
最終的圖像展現使用plt.show()方法。工具
返回:佈局
axes[0][0]
;參數:spa
非規則的排列,使用subplot2grid(),經過結構shape
和位置loc
排列子圖,如,排列5個子圖,上三下二結構:code
ax1 = plt.subplot2grid(shape=(2, 6), loc=(0, 0), colspan=2)
ax2 = plt.subplot2grid((2, 6), (0, 2), colspan=2)
ax3 = plt.subplot2grid((2, 6), (0, 4), colspan=2)
ax4 = plt.subplot2grid((2, 6), (1, 1), colspan=2)
ax5 = plt.subplot2grid((2, 6), (1, 3), colspan=2)
複製代碼
在排列子圖的過程當中,可能出現:orm
tight_layout()
設置間距,其中pad
表示總體輪廓間距,w_pad
表示子圖水平間距,h_pad
表示子圖豎直間距;set_size_inches()
設置圖像長寬的具體尺寸(英寸);即:cdn
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
fig = plt.gcf()
fig.set_size_inches(10, 8)
複製代碼
這樣,子圖就能夠優雅地排列在一塊兒了。
連續信號推薦使用specgram()展現,即光譜圖(spectrogram)。
參數:
即:
axes[0, 0].specgram(x=y, Fs=sr) # 光譜圖
axes[0, 0].set_title('std')
複製代碼
set_title()
用於設置圖像的標題。
連續信號不推薦使用線圖展現,由於在信號中,通常含有幀率,即每秒信號數,而使用線圖展現,就會忽略幀率信息,沒法體現波形的類似性。
二維特徵推薦使用pcolormesh()展現。除此以外,類似的展現形式:
即
axes[0, 0].pcolormesh(df1)
axes[0, 1].contourf(df2)
axes[1, 0].imshow(df2, interpolation='bicubic', aspect='auto')
axes[1, 1].axis('off')
複製代碼
axis('off')
用於關閉子圖。
二維特徵不推薦使用散點圖,由於散點圖的展現區域較小,對比效果較差。
seaborn是Matplotlib的顏色和樣式擴展。
使用方式:在plt繪製以前,經過seaborn聲明其餘的展現樣式。
import seaborn as sns
sns.set(style='ticks', palette='Set2')
複製代碼
Matplotlibcolors,參考
標準直方圖的繪製,輸入數據hist_data
是列表數據。
def plot_hist(hist_data):
""" 繪製直方圖 :param hist_data: 直方圖數據 :return: 展現圖 """
hist_data = np.asarray(hist_data)
sns.set(style='ticks', palette='Set2')
fig, aux = plot.subplots(ncols=1, nrows=1)
aux.hist(hist_data, bins=50, facecolor='magenta', alpha=0.75)
plot.show()
複製代碼
對於座標軸X(或Y):
set_xlabel
設置標籤;set_xlim
設置座標值的範圍set_xticks
設置座標值的粒度;注意:座標值的範圍(lim)與粒度(ticks)儘可能統一。
aux.set_xlabel("sec")
aux.set_ylabel("num")
min_x, max_x = 8, 22
aux.set_xlim([min_x, max_x])
aux.set_xticks(range(min_x, max_x, 1))
複製代碼
歡迎Follow個人GitHub:https://github.com/SpikeKing
By C. L. Wang @ 美圖雲事業部
OK, that's all!