使用Matplotlib画多子图的方法和代码是什么
Admin 2022-09-07 群英技术资讯 997 次浏览
这篇文章给大家介绍了“使用Matplotlib画多子图的方法和代码是什么”的相关知识,讲解详细,步骤过程清晰,有一定的借鉴学习价值,因此分享给大家做个参考,感兴趣的朋友接下来一起跟随小编看看吧。通过获取子图的label和线型来合并图例
注意添加label
#导入数据(读者可忽略)
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'
# 生成一个时间序列 (读者可根据情况进行修改或删除)
time =pd.to_datetime(np.arange(0,21), unit='D',
origin=pd.Timestamp('2021-10-19'))
#创建画布
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('感染人数/人',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('感染人数/人',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('感染人数/人',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('感染人数/人',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和线型数组
lines=[]
labels=[]
#通过循环获取线型和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)
结果如下图

这个时候我们再把原先代码里面的通过循环获取label和线型注释掉,代码如下
#导入数据(读者可忽略)
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'
# 生成一个时间序列 (读者可根据情况进行修改或删除)
time =pd.to_datetime(np.arange(0,21), unit='D',
origin=pd.Timestamp('2021-10-19'))
#创建画布
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('感染人数/人',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('感染人数/人',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('感染人数/人',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('感染人数/人',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和线型数组
# lines=[]
# labels=[]
#通过循环获取线型和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为子图之间高间距
对比图如下
设置了间距的图像

没有设置间距的图像

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本篇文章给大家带来了关于Python的相关知识,其中主要整理了编码规范的相关问题,想要写好python代码,必须了解python相关编码规范,有了这个的加持,编写的代码不仅可以实现相应的
这篇文章主要为大家介绍了R语言条形图及分布密度图代码总结,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
bool() 函数用于将给定参数转换为布尔类型,如果没有参数,返回 False。 bool 是 int 的子类。
python模块重载的方法有哪些?一些新手对于python模块重载的方法不是很了解,对此下面给大家分享几个python模块重载的方法,供大家参考,希望能对大家学习Python有帮助。
用python怎样实现图片转字符画的效果?相信不少朋友应该都有看过用一些字符组成起来图片的,放大来看是一个字符,全图来看又是一副完整图片,是不是很有意思呢?那么这样的效果是怎样实现的呢?下面和小编一起来看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008