Python Matplotlib繪制多子圖詳解
更新時間:2022年02月21日 10:22:09 作者:青石橫刀策馬
Matplotlib是Python中最受歡迎的數(shù)據(jù)可視化軟件包之一,它是 Python常用的2D繪圖庫,同時它也提供了一部分3D繪圖接口。本文將詳細介紹如何通過Matplotlib繪制多子圖,以及合并圖例和調整子圖間距,需要的可以參考一下
通過獲取子圖的label和線型來合并圖例
注意添加label
#導入數(shù)據(jù)(讀者可忽略)
pre_lp=total_res#組合模型
true=diff1[-pre_day:]#真實值
pre_ph=results_data["yhat"]#prophet
pre_lstm=reslut#lstm
pre_ari=data_ari['data_pre']#arima
#設置中文字體
rcParams['font.sans-serif'] = 'kaiti'
# 生成一個時間序列 (讀者可根據(jù)情況進行修改或刪除)
time =pd.to_datetime(np.arange(0,21), unit='D',
origin=pd.Timestamp('2021-10-19'))
#創(chuàng)建畫布
fig=plt.figure(figsize=(20,16))#figsize為畫布大小
# 1
ax1=fig.add_subplot(221)
ax1.plot(time,pre_lp,color='#1bb9f6',marker='^',linestyle='-',label='1')
# ax1.plot(time,true,color='#fd5749',marker='s',linestyle='-',label='true')
ax1.set_title('1',fontsize=15)#設置標題
ax1.set_xlabel('日期/天',fontsize=15)#設置橫坐標名稱
ax1.set_ylabel('感染人數(shù)/人',fontsize=15)#設置縱坐標名稱
ax1.xaxis.set_major_formatter(mdate.DateFormatter('%m-%d'))#設置橫坐標刻度(讀者可忽略)
plt.xticks(pd.date_range(time[0],time[-1],freq='D'),rotation=45)#設置橫坐標刻度(讀者可忽略)
# 2
ax2=fig.add_subplot(222)
ax2.plot(time,pre_ph,color='#739b06',marker='o',linestyle='-',label='2')
# ax2.plot(time,true,color='#fd5749',marker='s',linestyle='-',label='true')
ax2.set_title('2',fontsize=15)
ax2.set_xlabel('日期/天',fontsize=15)
ax2.set_ylabel('感染人數(shù)/人',fontsize=15)
ax2.xaxis.set_major_formatter(mdate.DateFormatter('%m-%d'))
plt.xticks(pd.date_range(time[0],time[-1],freq='D'),rotation=45)
# 3
ax3=fig.add_subplot(223)
ax3.plot(time,pre_lstm,color='#38d9a9',marker='*',linestyle='-',label='3')
# ax3.plot(time,true,color='#fd5749',marker='s',linestyle='-',label='true')
ax3.set_title('3',fontsize=15)
ax3.set_xlabel('日期/天',fontsize=15)
ax3.set_ylabel('感染人數(shù)/人',fontsize=15)
ax3.xaxis.set_major_formatter(mdate.DateFormatter('%m-%d'))
plt.xticks(pd.date_range(time[0],time[-1],freq='D'),rotation=45)
# 4
ax4=fig.add_subplot(224)
ax4.plot(time,pre_ari,color='#e666ff',marker='x',linestyle='-',label='4')
ax4.plot(time,true,color='#fd5749',marker='s',linestyle='-',label='true')
ax4.set_title('4',fontsize=15)
ax4.set_xlabel('日期/天',fontsize=15)
ax4.set_ylabel('感染人數(shù)/人',fontsize=15)
ax4.xaxis.set_major_formatter(mdate.DateFormatter('%m-%d'))
plt.xticks(pd.date_range(time[0],time[-1],freq='D'),rotation=45)
#初始化labels和線型數(shù)組
lines=[]
labels=[]
#通過循環(huán)獲取線型和labels
for ax in fig.axes:
axLine, axLabel = ax.get_legend_handles_labels()
lines.extend(axLine)
labels.extend(axLabel)
#設置圖例和調整圖例位置
fig.legend(lines, labels,loc='lower center',
ncol=5,framealpha=False,fontsize=25)結果如下圖

這個時候我們再把原先代碼里面的通過循環(huán)獲取label和線型注釋掉,代碼如下
#導入數(shù)據(jù)(讀者可忽略)
pre_lp=total_res#組合模型
true=diff1[-pre_day:]#真實值
pre_ph=results_data["yhat"]#prophet
pre_lstm=reslut#lstm
pre_ari=data_ari['data_pre']#arima
#設置中文字體
rcParams['font.sans-serif'] = 'kaiti'
# 生成一個時間序列 (讀者可根據(jù)情況進行修改或刪除)
time =pd.to_datetime(np.arange(0,21), unit='D',
origin=pd.Timestamp('2021-10-19'))
#創(chuàng)建畫布
fig=plt.figure(figsize=(20,16))#figsize為畫布大小
# 1
ax1=fig.add_subplot(221)
ax1.plot(time,pre_lp,color='#1bb9f6',marker='^',linestyle='-',label='1')
ax1.plot(time,true,color='#fd5749',marker='s',linestyle='-',label='true')
ax1.set_title('1',fontsize=15)#設置標題
ax1.set_xlabel('日期/天',fontsize=15)#設置橫坐標名稱
ax1.set_ylabel('感染人數(shù)/人',fontsize=15)#設置縱坐標名稱
ax1.xaxis.set_major_formatter(mdate.DateFormatter('%m-%d'))#設置橫坐標刻度(讀者可忽略)
plt.xticks(pd.date_range(time[0],time[-1],freq='D'),rotation=45)#設置橫坐標刻度(讀者可忽略)
# 2
ax2=fig.add_subplot(222)
ax2.plot(time,pre_ph,color='#739b06',marker='o',linestyle='-',label='2')
ax2.plot(time,true,color='#fd5749',marker='s',linestyle='-',label='true')
ax2.set_title('2',fontsize=15)
ax2.set_xlabel('日期/天',fontsize=15)
ax2.set_ylabel('感染人數(shù)/人',fontsize=15)
ax2.xaxis.set_major_formatter(mdate.DateFormatter('%m-%d'))
plt.xticks(pd.date_range(time[0],time[-1],freq='D'),rotation=45)
# 3
ax3=fig.add_subplot(223)
ax3.plot(time,pre_lstm,color='#38d9a9',marker='*',linestyle='-',label='3')
ax3.plot(time,true,color='#fd5749',marker='s',linestyle='-',label='true')
ax3.set_title('3',fontsize=15)
ax3.set_xlabel('日期/天',fontsize=15)
ax3.set_ylabel('感染人數(shù)/人',fontsize=15)
ax3.xaxis.set_major_formatter(mdate.DateFormatter('%m-%d'))
plt.xticks(pd.date_range(time[0],time[-1],freq='D'),rotation=45)
# 4
ax4=fig.add_subplot(224)
ax4.plot(time,pre_ari,color='#e666ff',marker='x',linestyle='-',label='4')
ax4.plot(time,true,color='#fd5749',marker='s',linestyle='-',label='true')
ax4.set_title('4',fontsize=15)
ax4.set_xlabel('日期/天',fontsize=15)
ax4.set_ylabel('感染人數(shù)/人',fontsize=15)
ax4.xaxis.set_major_formatter(mdate.DateFormatter('%m-%d'))
plt.xticks(pd.date_range(time[0],time[-1],freq='D'),rotation=45)
#初始化labels和線型數(shù)組
# lines=[]
# labels=[]
#通過循環(huán)獲取線型和labels
# for ax in fig.axes:
# axLine, axLabel = ax.get_legend_handles_labels()
# lines.extend(axLine)
# labels.extend(axLabel)
#設置圖例和調整圖例位置
fig.legend(lines, labels,loc='lower center',
ncol=5,framealpha=False,fontsize=25)結果如下圖

調整子圖間距
plt.subplots_adjust(wspace=0.4,hspace=0.4)
wspace為子圖之間寬間距,hspace為子圖之間高間距
對比圖如下
設置了間距的圖像

沒有設置間距的圖像

到此這篇關于Python Matplotlib繪制多子圖詳解的文章就介紹到這了,更多相關Python Matplotlib多子圖內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python 判斷是否為正小數(shù)和正整數(shù)的實例
這篇文章主要介紹了python 判斷是否為正小數(shù)和正整數(shù)的實例的相關資料,這里提供實例,實例注釋說明很清楚,需要的朋友可以參考下2017-07-07
Python實現(xiàn)PDF文字識別提取并寫入CSV文件
這篇文章主要是和大家分享一個Python實現(xiàn)PDF文字識別與提取并寫入?CSV文件的腳本。文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2022-03-03

