Python Matplotlib繪制多子圖詳解
通過獲取子圖的label和線型來合并圖例
注意添加label
#導(dǎo)入數(shù)據(jù)(讀者可忽略) pre_lp=total_res#組合模型 true=diff1[-pre_day:]#真實(shí)值 pre_ph=results_data["yhat"]#prophet pre_lstm=reslut#lstm pre_ari=data_ari['data_pre']#arima #設(shè)置中文字體 rcParams['font.sans-serif'] = 'kaiti' # 生成一個(gè)時(shí)間序列 (讀者可根據(jù)情況進(jìn)行修改或刪除) 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)#設(shè)置標(biāo)題 ax1.set_xlabel('日期/天',fontsize=15)#設(shè)置橫坐標(biāo)名稱 ax1.set_ylabel('感染人數(shù)/人',fontsize=15)#設(shè)置縱坐標(biāo)名稱 ax1.xaxis.set_major_formatter(mdate.DateFormatter('%m-%d'))#設(shè)置橫坐標(biāo)刻度(讀者可忽略) plt.xticks(pd.date_range(time[0],time[-1],freq='D'),rotation=45)#設(shè)置橫坐標(biāo)刻度(讀者可忽略) # 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) #設(shè)置圖例和調(diào)整圖例位置 fig.legend(lines, labels,loc='lower center', ncol=5,framealpha=False,fontsize=25)
結(jié)果如下圖
這個(gè)時(shí)候我們?cè)侔言却a里面的通過循環(huán)獲取label和線型注釋掉,代碼如下
#導(dǎo)入數(shù)據(jù)(讀者可忽略) pre_lp=total_res#組合模型 true=diff1[-pre_day:]#真實(shí)值 pre_ph=results_data["yhat"]#prophet pre_lstm=reslut#lstm pre_ari=data_ari['data_pre']#arima #設(shè)置中文字體 rcParams['font.sans-serif'] = 'kaiti' # 生成一個(gè)時(shí)間序列 (讀者可根據(jù)情況進(jìn)行修改或刪除) 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)#設(shè)置標(biāo)題 ax1.set_xlabel('日期/天',fontsize=15)#設(shè)置橫坐標(biāo)名稱 ax1.set_ylabel('感染人數(shù)/人',fontsize=15)#設(shè)置縱坐標(biāo)名稱 ax1.xaxis.set_major_formatter(mdate.DateFormatter('%m-%d'))#設(shè)置橫坐標(biāo)刻度(讀者可忽略) plt.xticks(pd.date_range(time[0],time[-1],freq='D'),rotation=45)#設(shè)置橫坐標(biāo)刻度(讀者可忽略) # 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) #設(shè)置圖例和調(diào)整圖例位置 fig.legend(lines, labels,loc='lower center', ncol=5,framealpha=False,fontsize=25)
結(jié)果如下圖
調(diào)整子圖間距
plt.subplots_adjust(wspace=0.4,hspace=0.4)
wspace為子圖之間寬間距,hspace為子圖之間高間距
對(duì)比圖如下
設(shè)置了間距的圖像
沒有設(shè)置間距的圖像
到此這篇關(guān)于Python Matplotlib繪制多子圖詳解的文章就介紹到這了,更多相關(guān)Python Matplotlib多子圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Python實(shí)現(xiàn)外觀數(shù)列求解
這篇文章主要介紹了利用Python實(shí)現(xiàn)外觀數(shù)列求解,文章利用舉例說明文章的主題內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴樂意參考一下2022-03-03python 判斷是否為正小數(shù)和正整數(shù)的實(shí)例
這篇文章主要介紹了python 判斷是否為正小數(shù)和正整數(shù)的實(shí)例的相關(guān)資料,這里提供實(shí)例,實(shí)例注釋說明很清楚,需要的朋友可以參考下2017-07-07Python使用requests庫進(jìn)行請(qǐng)求重試
在進(jìn)行網(wǎng)絡(luò)請(qǐng)求時(shí),由于網(wǎng)絡(luò)波動(dòng)、服務(wù)器繁忙等原因,可能會(huì)出現(xiàn)請(qǐng)求失敗的情況,為了提高請(qǐng)求的成功率,我們可以使用請(qǐng)求重試機(jī)制,本文就來講講如何在 Python 中使用 requests 庫進(jìn)行請(qǐng)求重試吧2023-06-06一文帶你了解Python列表生成式應(yīng)用的八重境界
在Python中有非常多且好用的技巧,其中使用最多的是列表生成式,往往可以將復(fù)雜的邏輯用簡(jiǎn)單的語言來實(shí)現(xiàn),本文重點(diǎn)介紹列表生成式應(yīng)用的八重境界2022-09-09Python虛擬機(jī)中描述器的王炸應(yīng)用分享
本篇文章給大家介紹一下描述器在?python?語言當(dāng)中有哪些應(yīng)用,主要介紹如何使用?python?語言實(shí)現(xiàn)?python?內(nèi)置的?proterty?、staticmethod?和?class?method,需要的可以參考一下2023-05-05Python實(shí)現(xiàn)PDF文字識(shí)別提取并寫入CSV文件
這篇文章主要是和大家分享一個(gè)Python實(shí)現(xiàn)PDF文字識(shí)別與提取并寫入?CSV文件的腳本。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-03-03