利用Matplotlib实现单画布绘制多个子图
作者:杨哥学编程 发布时间:2021-10-30 19:33:58
标签:Matplotlib,单画布,绘制,子图
Matplotlib实现单画布绘制多个子图
最近研究Python数据分析,需要利用Matplotlib绘制图表,并将多个图表绘制在一张图中,经过一番折腾,利用matplotlib包下的subplot()
函数即可实现此功能。
代码实现:
import matplotlib.pyplot as plt
import numpy as np
class Graph(object):
def __init__(self):
self.font = {
'size': 13
}
plt.figure(figsize=(9, 6))
plt.subplots_adjust(wspace=0.7, hspace=0.5)
plt.rcParams['font.family'] = 'simhei'
plt.rcParams['axes.unicode_minus'] = False
def twinx(self):
a1 = plt.subplot(231)
plt.title('双纵轴折线图', fontdict=self.font)
a1.plot(subjects, v1, label='v1')
a1.set_ylabel('v1')
a1.legend(loc='upper right', bbox_to_anchor=[-0.5, 0, 0.5, 1], fontsize=7)
a2 = a1.twinx()
a2.plot(subjects, v2, 'r--', label='v2')
a2.set_ylabel('v2')
a2.legend(loc='upper left', bbox_to_anchor=[1, 0, 0.5, 1], fontsize=7)
def scatter(self):
plt.subplot(232)
plt.title('散点图', fontdict=self.font)
x = range(50)
y_jiangsu = [np.random.uniform(15, 25) for i in x]
y_beijing = [np.random.uniform(5, 18) for i in x]
plt.scatter(x, y_beijing, label='v1')
plt.scatter(x, y_jiangsu, label='v2')
plt.legend(loc='upper left', bbox_to_anchor=[1, 0, 0.5, 1], fontsize=7)
def hist(self):
plt.subplot(233)
plt.title('直方图', fontdict=self.font)
x = np.random.normal(size=100)
plt.hist(x, bins=30)
def bar_dj(self):
plt.subplot(234)
plt.title('堆积柱状图', fontdict=self.font)
plt.bar(np.arange(len(v1)), v1, width=0.6, label='v1')
for x, y in enumerate(v1):
plt.text(x, y, y, va='top', ha='center')
plt.bar(np.arange(len(v2)), v2, width=0.6, bottom=v1, label='v2')
for x, y in enumerate(v2):
plt.text(x, y + 60, y, va='bottom', ha='center')
plt.ylim(0, 200)
plt.legend(loc='upper left', bbox_to_anchor=[1, 0, 0.5, 1], fontsize=7)
plt.xticks(np.arange(len(v1)), subjects)
def bar_bl(self):
plt.subplot(235)
plt.title('并列柱状图', fontdict=self.font)
plt.bar(np.arange(len(v1)), v1, width=0.4, color='tomato', label='v1')
for x, y in enumerate(v1):
plt.text(x - 0.2, y, y)
plt.bar(np.arange(len(v2)) + 0.4, v2, width=0.4, color='steelblue', label='v2')
for x, y in enumerate(v2):
plt.text(x + 0.2, y, y)
plt.ylim(0, 110)
plt.xticks(np.arange(len(v1)), subjects)
plt.legend(loc='upper left', bbox_to_anchor=[1, 0, 0.5, 1], fontsize=7)
def barh(self):
plt.subplot(236)
plt.title('水平柱状图', fontdict=self.font)
plt.barh(np.arange(len(v1)), v1, height=0.4, label='v1')
plt.barh(np.arange(len(v2)) + 0.4, v2, height=0.4, label='v2')
plt.legend(loc='upper left', bbox_to_anchor=[1, 0, 0.5, 1], fontsize=7)
plt.yticks(np.arange(len(v1)), subjects)
def main():
g = Graph()
g.twinx()
g.scatter()
g.hist()
g.bar_dj()
g.bar_bl()
g.barh()
plt.savefig('坐标轴类.png')
plt.show()
if __name__ == '__main__':
subjects = ['语文', '数学', '英语', '物理', '化学']
v1 = [77, 92, 83, 74, 90]
v2 = [63, 88, 99, 69, 66]
main()
效果如下:
可以看到,一个画板上放了6个子图。达到了我们想要的效果。
现在来解析刚刚的部分代码:
plt.figure(1)
:表示取第一块画板,一个画板就是一张图,如果你有多个画板,那么最后就会弹出多张图。plt.subplot(231)
:221表示将画板划分为2行3列,然后取第1个区域。那么第几个区域是怎么界定的呢?这个规则遵循行优先数数规则.优先从行开始数,从左到右按顺序1234……然后再下一行。
Matplotlib绘制多个动态子图
import os
import cv2
import pytz
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib.gridspec import GridSpec
from datetime import datetime
# (200,125) ,(300,185)
def ave_area(arrays, left_top=(350, 180), right_lower=(400,255)):
np_array = arrays[left_top[0]:right_lower[0], left_top[1]:right_lower[1]].reshape(1, -1)
delete_0 = np_array[np_array != 0]
return np.mean(delete_0) / 1000
img_depths_x = []
img_depths_y = []
img_colors = []
dirs = r'Z:\10.1.22.215\2021-09-09-18'
for file in tqdm(os.listdir(dirs)[4000:4400]):
try:
img_path = os.path.join(dirs, file)
data = np.load(img_path, allow_pickle=True)
depthPix, colorPix = data['depthPix'], data['colorPix']
#rgbimage = cv2.cvtColor(colorPix, cv2.COLOR_BGR2RGB)
font = cv2.FONT_HERSHEY_SIMPLEX
text = file.replace('.npz', '')
cv2.putText(colorPix, text, (10, 30), font, 0.75, (0, 0, 255), 2)
cv2.putText(depthPix, text, (10, 30), font, 0.75, (0, 0, 255), 2)
#cv2.imshow('example', colorPix)
cv2.waitKey(10)
indexes = file.replace('.npz', '')
key = datetime.strptime(indexes, '%Y-%m-%d-%H-%M-%S-%f').astimezone(pytz.timezone('Asia/ShangHai')).timestamp() #格式时间转换
img_depths_x.append(key)
img_depths_y.append(ave_area(depthPix))
img_colors.append(cv2.cvtColor(colorPix,cv2.COLOR_BGR2RGB))
except:
continue
fig = plt.figure(dpi=100,
constrained_layout=True, # 类似于tight_layout,使得各子图之间的距离自动调整【类似excel中行宽根据内容自适应】
figsize=(15, 12)
)
gs = GridSpec(3, 1, figure=fig)#GridSpec将fiure分为3行3列,每行三个axes,gs为一个matplotlib.gridspec.GridSpec对象,可灵活的切片figure
ax1 = fig.add_subplot(gs[0:2, 0])
ax2 = fig.add_subplot(gs[2:3, 0])
xdata, ydata = [], []
rect = plt.Rectangle((350, 180), 75, 50, fill=False, edgecolor = 'red',linewidth=1)
ax1.add_patch(rect)
ln1 = ax1.imshow(img_colors[0])
ln2, = ax2.plot([], [], lw=2)
def init():
ax2.set_xlim(img_depths_x[0], img_depths_x[-1])
ax2.set_ylim(12, 14.5)
return ln1, ln2
def update(n):
ln1.set_array(img_colors[n])
xdata.append(img_depths_x[n])
ydata.append(img_depths_y[n])
ln2.set_data(xdata, ydata)
return ln1, ln2
ani = animation.FuncAnimation(fig,
update,
frames=range(len(img_depths_x)),
init_func=init,
blit=True)
ani.save('vis.gif', writer='imagemagick', fps=10)
来源:https://blog.csdn.net/qq_47183158/article/details/112747898


猜你喜欢
- subplot函数介绍matplotlib下, 一个 Figure 对象可以包含多个子图(Axes), 可以使用 subplot() 快速绘
- 导入相关包import timeimport pydashimport base64import requestsfrom lxml imp
- 在vue项目中, mock数据可以使用 node 的 express模块搭建服务1. 在根目录下创建 test 目录, 用来存放模拟的 js
- 前言本文主要介绍的是基于centos7进行yum安装lnmp(linux+nginx+php7.1+mysql5.7)的相关教程,文中将一步
- 1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere people
- 如下所示:depot_name = models.CharField( u'设备库房名称', bla
- python类中定义的函数称为方法, init ()方法本质也是一个函数。这个函数的作用是初始化实例后的对象。具体如下例:init
- 当两个数包含小数进行运算的时候结果并不是正确的结果,而是出现了精度丢失的情况(小数点后面出现很多位)。问题所在:res.orderColor
- 1 事务的使用1.1 事务概念事务就是一组DML语句组成,这些语句在逻辑上存在相关性,这一组DML语句要么全部成功,要么全部失败,是一个整体
- 背景客户最近有这样的需求,想通过统计Oracle数据库活跃会话数,并记录在案,利用比对历史的活跃会话的方式,实现对系统整体用户并发量有大概的
- django中form表单设置action后,点提交按钮是跳转到action页面的,比如设置action为login,网址为192.168.
- 游标是 * 的!在关系数据库中,我们对于查询的思考是面向集合的。而游标打破了这一规则,游标使得我们思考方式变为逐行进行.对于类C的开发人员来着
- 一、方框滤波 方框滤波是均值滤波的一种形式。在均值滤波中,滤波结果的像素值是任意一个点的邻域平均值,等于各邻域像素值之和的均值,而在方框
- 一般数据库默认是10次尝试失败后锁住用户 1、查看FAILED_LOGIN_ATTEMPTS的值selec
- ConfigParser库的使用及遇到的坑背景:这几天想在接口测试中增加logging打印功能,在testerHome正好发现有人分享自己的
- 数在 Python 中,对数的规定比较简单,基本在小学数学水平即可理解。那么,做为零基础学习这,也就从计算小学数学题目开始吧。因为从这里开始
- vue封装常用工具类公司要新开一个项目,我来分享一下简单封装常用的工具类首先在util目录下创建一个Common.js文件然后开始封装1.验
- 当我们需要批量删除数据库中的表时,对于单个删除一些表是否感到烦躁,厌倦,干脆写个脚本用得了。本脚本使用游标循环删除,对于数量比较小,用游标暂
- 微信小程序可滑动月日历组件此日历可进行左右滑动,展示签到打卡信息,和大家分享一下。如果样式变形,请检查是否有共用样式起冲突展示一下效果图在c
- 本文旨在讲述 RPC 框架设计中的几个核心问题及其解决方法,并基于 Golang 反射技术,构建了一个简易的 RPC 框架。项目地址:Tin