gpt4 book ai didi

python - 在 wxpython 中嵌入实时更新的 matplotlib 图

转载 作者:太空宇宙 更新时间:2023-11-04 01:20:30 26 4
gpt4 key购买 nike

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

关于python - 在 wxpython 中嵌入实时更新的 matplotlib 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21579658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com