Python Matplotlib绘制多子图详解
作者:青石横刀策马 发布时间:2021-01-11 11:27:05
标签:Python,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为子图之间高间距
对比图如下
设置了间距的图像
没有设置间距的图像
来源:https://blog.csdn.net/m0_53115174/article/details/123008276


猜你喜欢
- 描述Bootstrap Button(按钮)JavaScript 插件允许您加强按钮的功能。您可以控制按钮的状态,也可以为组件创建按钮组,比
- <!doctype html> <html> <head> <meta content="
- 本文实例讲述了Python基于回溯法子集树模板实现图的遍历功能。分享给大家供大家参考,具体如下:问题一个图:A --> BA --&g
- Ajax,全称为Asynchronous JavaScript and XML,即异步的JavaScript和XML。它不是一门编程语言,而
- 表单外观的美化很多时候,我们仅仅为了实现数据采集这个功能来使用表单,常看到的表单都是“千人一面”、毫无
- 如图:其中Num是自增长列,Operation是分类标签,count是汇总数据 代码如下:select Num=row_numb
- 学习Python的人都知道数组是最常用的的数据类型,为了保证程序的正确性,需要调试程序。因此,需要在程序中控制台中打印数组的全部元素,如果数
- 什么是ASP,它能干什么? 一、什么是ASP? 从字面上说,ASP包含三方面含义: 1、Active:ASP使用了Microsoft的Act
- 在实际工作或面试中,我们经常会遇到“数组去重”问题,接下来就是使用js实现的数组去重的多种方法:1.将数组的每一个元素依次与其他元素做比较,
- Mysql的分页的两个参数select * from user limit 1,21表示从第几条数据开始查(默认索引是0,如果写1,从第二条
- 一行命令搭建一个基于python的http文件传输服务由于今天朋友想要一个文件,而我恰好有,因为这个文件比较大,网速不是很给力,所以想到了p
- 如下所示:#coding=gbk'''GPU上面的环境变化太复杂,这里我直接给出在笔记本CPU上面的运行时间结果由于
- 1,下载Yii,站点:http://www.yiiframework.com/download/注意版本,这里是根据Yii1来的,如果是Yi
- 由于跑编码的需要,所以需要制作一个.yuv格式的图片数据集,但是手头只有.jpg格式的,故记录下转换过程。其他图片格式也可以,代码里修改一下
- 简介此样式基于bootstrap-3.3.0,样式文件里的源码行数都是指的这个版本.CSS源文件放到了Content文件夹下的bootstr
- 本文实例讲述了使用Python生成XML的方法。分享给大家供大家参考,具体如下:1. bookstore.py#encoding:utf-8
- python PIL图像处理模块中的ImageDraw类支持各种几何图形的绘制和文本的绘制,如直线、椭圆、弧、弦、多边形以及文字等。下面直接
- 表操作 例 1 对于表的教学管理数据库中的表 STUDENTS ,可以定义如
- 操作步骤:一、安装MySQL数据库1、下载MySQL-5.6.17-winx64.zip文件。2、解压到指定目录,本例为D:\mysql-5
- 直接上代码import pygameimport randomdef main(): # 初始化pygame &n