最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python Matplotlib繪制多子圖詳解

 更新時(shí)間:2022年02月21日 10:22:09   作者:青石橫刀策馬  
Matplotlib是Python中最受歡迎的數(shù)據(jù)可視化軟件包之一,它是 Python常用的2D繪圖庫(kù),同時(shí)它也提供了一部分3D繪圖接口。本文將詳細(xì)介紹如何通過(guò)Matplotlib繪制多子圖,以及合并圖例和調(diào)整子圖間距,需要的可以參考一下

通過(guò)獲取子圖的label和線(xiàn)型來(lái)合并圖例

注意添加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)建畫(huà)布
fig=plt.figure(figsize=(20,16))#figsize為畫(huà)布大小
# 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)名稱(chēng)
ax1.set_ylabel('感染人數(shù)/人',fontsize=15)#設(shè)置縱坐標(biāo)名稱(chēng)
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和線(xiàn)型數(shù)組
lines=[]
labels=[]
#通過(guò)循環(huán)獲取線(xià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里面的通過(guò)循環(huán)獲取label和線(xiàn)型注釋掉,代碼如下

#導(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)建畫(huà)布
fig=plt.figure(figsize=(20,16))#figsize為畫(huà)布大小
# 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)名稱(chēng)
ax1.set_ylabel('感染人數(shù)/人',fontsize=15)#設(shè)置縱坐標(biāo)名稱(chēng)
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和線(xiàn)型數(shù)組
# lines=[]
# labels=[]
#通過(guò)循環(huán)獲取線(xià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è)置了間距的圖像

沒(méi)有設(shè)置間距的圖像

到此這篇關(guān)于Python Matplotlib繪制多子圖詳解的文章就介紹到這了,更多相關(guān)Python Matplotlib多子圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • numpy自動(dòng)生成數(shù)組詳解

    numpy自動(dòng)生成數(shù)組詳解

    這篇文章主要介紹了numpy自動(dòng)生成數(shù)組詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Python獲取request response body的方法

    Python獲取request response body的方法

    本文介紹了使用Python的requests庫(kù)發(fā)送HTTP GET請(qǐng)求并獲取響應(yīng)體的方法,同時(shí),還簡(jiǎn)要介紹了如何使用Flask框架在Python中創(chuàng)建一個(gè)簡(jiǎn)單的網(wǎng)站,感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • Python實(shí)現(xiàn)掃碼工具的示例代碼

    Python實(shí)現(xiàn)掃碼工具的示例代碼

    這篇文章主要介紹了Python實(shí)現(xiàn)掃碼工具的示例代碼,代碼簡(jiǎn)單易懂對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 如何使用pandas讀取txt文件中指定的列(有無(wú)標(biāo)題)

    如何使用pandas讀取txt文件中指定的列(有無(wú)標(biāo)題)

    這篇文章主要介紹了如何使用pandas讀取txt文件中指定的列(有無(wú)標(biāo)題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • python與php實(shí)現(xiàn)分割文件代碼

    python與php實(shí)現(xiàn)分割文件代碼

    本文給大家分享的是兩個(gè)分別使用python和php實(shí)現(xiàn)的將文件分割成小文件的代碼,非常的實(shí)用有需要的小伙伴可以參考下
    2017-03-03
  • Django中的settings.py文件使用全解析

    Django中的settings.py文件使用全解析

    這篇文章主要介紹了Django中的settings.py文件使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Python文件如何引入?詳解引入Python文件步驟

    Python文件如何引入?詳解引入Python文件步驟

    我們整理了一篇關(guān)于引入Python文件的一個(gè)基礎(chǔ)知識(shí)點(diǎn)內(nèi)容,如果你是一個(gè)python的學(xué)習(xí)者,參考一下吧。
    2018-12-12
  • Python3基于print打印帶顏色字符串

    Python3基于print打印帶顏色字符串

    這篇文章主要介紹了Python3使基于print打印帶顏色字符串,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Python繼承和子類(lèi)從Person到Student演示實(shí)例

    Python繼承和子類(lèi)從Person到Student演示實(shí)例

    這篇文章主要為大家介紹了Python繼承和子類(lèi)從Person到Student演示實(shí)例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Python使用 Beanstalkd 做異步任務(wù)處理的方法

    Python使用 Beanstalkd 做異步任務(wù)處理的方法

    這篇文章主要介紹了Python使用 Beanstalkd 做異步任務(wù)處理的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04

最新評(píng)論

临高县| 临夏市| 阳山县| 炎陵县| 保德县| 武胜县| 公主岭市| 夏河县| 大港区| 崇礼县| 株洲县| 夏邑县| 祁阳县| 米泉市| 稻城县| 巫山县| 无为县| 新田县| 镇平县| 锡林郭勒盟| 衡阳市| 闽侯县| 连平县| 松潘县| 厦门市| 洛浦县| 敦煌市| 太白县| 霍城县| 临武县| 安阳县| 广汉市| 武隆县| 汕头市| 垦利县| 宁夏| 富顺县| 五常市| 汝南县| 静海县| 石城县|