我是靠谱客的博主 成就书包,最近开发中收集的这篇文章主要介绍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.2152021-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)

最后

以上就是成就书包为你收集整理的matplotlib绘制多个动态子图的全部内容,希望文章能够帮你解决matplotlib绘制多个动态子图所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部