Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在wxpython中嵌入实时更新matplotlib图_Python_Matplotlib_Wxwidgets - Fatal编程技术网

在wxpython中嵌入实时更新matplotlib图

在wxpython中嵌入实时更新matplotlib图,python,matplotlib,wxwidgets,Python,Matplotlib,Wxwidgets,我是wx python的新手。以下是从可实时更新的文本文件绘制实时图形的代码。谁能帮我把这个代码嵌入到wx框架中。我的项目非常需要它 import matplotlib.pyplot as plt import matplotlib.animation as animation import time fig= plt.figure() ax1=fig.add_subplot(1,1,1) def animate(i): pullData= open('C:/test/e.txt

我是wx python的新手。以下是从可实时更新的文本文件绘制实时图形的代码。谁能帮我把这个代码嵌入到wx框架中。我的项目非常需要它

import matplotlib.pyplot as plt  
import matplotlib.animation as animation
import time

fig= plt.figure()
ax1=fig.add_subplot(1,1,1)

def animate(i):
    pullData= open('C:/test/e.txt','r').read()
    dataArray= pullData.split('\n')
    xar=[]
    yar=[]
    for eachLine in dataArray:
        if len(eachLine)>1:
            x,y= eachLine.split(',')
            xar.append(int(x)) 
            yar.append(int(y))
    ax1.clear()
    ax1.plot(xar,yar)
ani= animation.FuncAnimation(fig,animate, interval=1000)
plt.show()

在这里,我将给出一个示例,但您需要根据需要更改绘图部分:

import wx
import numpy as np
import matplotlib.figure as mfigure
import matplotlib.animation as manim

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg

class MyFrame(wx.Frame):
    def __init__(self):
        super(MyFrame,self).__init__(None, wx.ID_ANY, size=(800, 600))
        self.fig = mfigure.Figure()
        self.ax = self.fig.add_subplot(111)
        self.canv = FigureCanvasWxAgg(self, wx.ID_ANY, self.fig)
        self.values = []
        self.animator = manim.FuncAnimation(self.fig,self.anim, interval=1000)

    def anim(self,i):
        if i%10 == 0:
            self.values = []
        else:
            self.values.append(np.random.rand())
        self.ax.clear()
        self.ax.set_xlim([0,10])
        self.ax.set_ylim([0,1])        
        return self.ax.plot(np.arange(1,i%10+1),self.values,'d-')


wxa = wx.PySimpleApp()
w = MyFrame()
w.Show(True)
wxa.MainLoop()
这里有一些很好的例子。