当前位置:   article > 正文

python多线程实现绘制动态图_python动态

python动态

一、背景

  • 有些情况下,我们面对实时更新的数据,希望能够在一个窗口中可视化出来,并且能够实时更新,方便我们观察数据的变化,从而进行数据分析,例如:绘制音频的波形,绘制动态曲线等,下面介绍使用matplotlib结合多线程绘制动态图,希望能帮助到有需要的朋友。
  • 本人遇到的场景:最近刚好在学习人工智能中的遗传算法,并且使用该算法求解TSP,了解这个算法的朋友知道这个算法是通过不断迭代,寻找适应度大的最优解,为了了解迭代过程中适应度的变化,我希望能够实时更新迭代过程中的适应度,将其可视化出来(数据量不断增大)
  • 直接上图:
    在这里插入图片描述

二、步骤

1、使用matplotlib绘制动态图

  • 工具:matplotlib.animation

2、创建一个线程用于更新数据

  • threading

三、代码框架

# Author: 浅若清风
# Date: 2020/12/11

import threading
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.lines as line
import numpy as np

CHUNK = 2048  # 初始数据量
data=np.random.normal(0,1,CHUNK)  # 存放数据,用于绘制图像,数据类型可为列表

# 定义画布
fig = plt.figure()
ax = plt.subplot(111,ylim=(0,5))
line = line.Line2D([], [])  # 绘制直线

# 初始化图像
def plot_init():
    ax.add_line(line)
    return line, # 必须加逗号,否则会报错(TypeError: 'Line2D' object is not iterable)

# 更新图像(animation会不断调用此函数刷新图像,实现动态图的效果)
def plot_update(i):
    global data  # data为全局变量
    data_copy = data.copy()  # 为避免线程不同步导致获取到的data在绘制图像时被更新,这里复制数据的副本,否则绘制图像的时候可能会出现x和y的数据维度不相等的情况
    x_data=np.arange(0,data_copy.shape[0],1)  # x轴根据y轴数据自动生成(可根据需要修改)
    ax.set_xlim(0,data_copy.shape[0])  # 横坐标范围(横坐标的范围和刻度可根据数据长度更新)
    ax.set_title("title",fontsize=8)  # 设置title
    line.set_xdata(x_data)  # 更新直线的数据
    line.set_ydata(data_copy)  # 更新直线的数据
	# 大标题(若有多个子图,可为其设置大标题)
    plt.suptitle('Suptitle',fontsize=8)
    # 重新渲染子图
    ax.figure.canvas.draw()  # 必须加入这一行代码,才能更新title和坐标!!!
    return line,  # 必须加逗号,否则会报错(TypeError: 'Line2D' object is not iterable)

# 绘制动态图
ani = animation.FuncAnimation(fig,   # 画布
							  plot_update,  # 图像更新
                              init_func=plot_init,  # 图像初始化
                              frames=1,
                              interval=30,  # 图像更新间隔
                              blit=True)

# 数据更新函数
def dataUpdate_thead():
    global data
    # 为了方便理解代码,这里生成正态分布的随机数据
    while True:  # 为了方便测试,让数据不停的更新
	    data=np.random.normal(0,1,CHUNK)

# 为数据更新函数单独创建一个线程,与图像绘制的线程并发执行
ad_rdy_ev = threading.Event()
ad_rdy_ev.set()  # 设置线程运行
t = threading.Thread(target=dataUpdate_thead, args=()) # 更新数据,参数说明:target是线程需要执行的函数,args是传递给函数的参数)
t.daemon = True
t.start()  # 线程执行

plt.show() # 显示图像(0,1,CHUNK)

# 为数据更新函数单独创建一个线程,与图像绘制的线程并发执行
ad_rdy_ev = threading.Event()
ad_rdy_ev.set()  # 设置线程运行
t = threading.Thread(target=dataUpdate_thead, args=()) # 更新数据,参数说明:target是线程需要执行的函数,args是传递给函数的参数)
t.daemon = True
t.start()  # 线程执行

plt.show() # 显示图像
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 效果:
    在这里插入图片描述
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/115921
推荐阅读
相关标签
  

闽ICP备14008679号