在wxpython中嵌入实时更新的matplotlib图 [英] Embedding a live updating matplotlib graph in wxpython

查看:114
本文介绍了在wxpython中嵌入实时更新的matplotlib图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 wx python 的新手.以下是从可实时更新的文本文件中绘制实时图形的代码.任何人都可以帮我将此代码嵌入到 wx 框架中.我的项目非常需要它.

I am a newbie into wx python. The following is the code to plot live graph from a text file which can be updated live. Can anybody please help me to embed this code into a wx frame. I desperately need it for my project.

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()

推荐答案

这里我会给你一个例子,但你需要根据你的需要更改绘图部分:

Here I'll give you an example but you need to change the plotting part for your needs:

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()

这篇关于在wxpython中嵌入实时更新的matplotlib图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆