1

我有这样的课


class WaveData(object):
    def __init__(self, data):
        self.data = data

并创建一个数据对象,绘制一个图形

wave = WaveData([[1, 2, 3], 
                 [7, 5, 6]])

import matplotlib.pyplot as plt
fig=plt.figure()
plot1, = fig.canvas.figure.subplots().plot(wave.data[0])
plot2, = fig.canvas.figure.subplots().plot(wave.data[1])

我希望当我改变波值时,情节会同步改变

wave.data[1]=[5,6,7] # hope figure change together

我尝试changedataWaveData类添加方法,但是:

  1. 它需要使用全局变量fig,可能不是reasonale(我可以将fig作为self属性,但实际上fig还链接了其他没有写在这里的类对象)
  2. 我不能通过fig直接更改数据来更改:wave.data[1] =[5,6,7]
class WaveData(object):
    def __init__(self, data):
        self.data = data

    def changedata(self,value,index):
        self.data[index]=value
        

        #-- change the plot index th plot data--#
        global plot1,plot2,fig
        plot1.set_ydata(self.data[1])
        plot2.set_ydata(self.data[2])
        fig.canvas.draw_idle()
        #-- change the plot index th plot data--#
     

我想创建一个观察者来监控wave.datavalue 。当检测到值变化时,执行一些动作

怎么做?

4

1 回答 1

0

右:绘图不是一个动态或交互的过程。您以正确的方式开始,使用更改波形的访问方法。现在您必须重新绘制并重新显示结果......这可能需要手动关闭第一个绘图,具体取决于您选择的绘图包的界面(例如matplotlib)。

为了获得完全交互的体验,您可能需要使用动画包,例如 PyGame,其中视觉显示的变化是包假设的一部分。

于 2020-09-22T05:02:20.067 回答