1.思路:
(1)利用FigureCanvasWxAgg嵌入canvas
(2)利用 matplotlib.figure 放入画布中,在figure中画出自己想要的图像
(3)动画利用matplotlib.animation实现,动画停止可以用stop停止
self.ani = animation.FuncAnimation(self.figure,
self.animate,
interval=1000,
blit=False,
init_func=self.init)
self.ani.event_source.stop()
(4)动态数据通过队列和异步线程放入queue中,利用animation取数据,然后重画图像。
(5)可视化显示数据,利用figure.canvas.mpl_connect来相应鼠标事件
self.axes.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)
1.1.鼠标事件
# 当鼠标在子图范围内产生动作时,将触发鼠标事件,鼠标事件分为三种:
botton_press_event: 鼠标按下时触发
botton_release_event: 鼠标释放时触发
motion_notify_event: 时间移动时触发
1.2.鼠标事件的相关信息可以通过event对象的属性获得:
name: 事件名
button: 鼠标按键,1,2,3表示左中右按键,None表示没有按键
x,y: 表示鼠标在图表中的像素坐标
xdata,ydata:鼠标在数据坐标系的坐标
"""
2实现效果:
3.完整代码如下,保证运行:
# -*- coding: UTF-8 -*-
import threading
import queue
import time
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
import wx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
class MyFrame(wx.Frame):
def __init__(self, parent):