wxPython-图形化数据

wxPython-图形化数据,python,wxpython,graphing,Python,Wxpython,Graphing,我在wxPython中绘制数据时遇到了一个问题——下面的代码可以工作,但并不完全正确:现在我绘制的随机数是输入框的倍数,每100ms绘制一次 我的问题是,数字的整个历史都显示为一个姿势,即(比如)25个样本的运行窗口。最初,我尝试在100个样本的每个集合上重新绘制图表,类似于,if(length(data)%100):drawGraph,但这看起来同样不正确 欢迎提出意见和建议 我的代码: print( "\n- Please Wait -- Importing Matplotlib and R

我在wxPython中绘制数据时遇到了一个问题——下面的代码可以工作,但并不完全正确:现在我绘制的随机数是输入框的倍数,每100ms绘制一次

我的问题是,数字的整个历史都显示为一个姿势,即(比如)25个样本的运行窗口。最初,我尝试在100个样本的每个集合上重新绘制图表,类似于,
if(length(data)%100):drawGraph
,但这看起来同样不正确

欢迎提出意见和建议

我的代码:

print( "\n- Please Wait -- Importing Matplotlib and Related Modules...\n" )
import random
import matplotlib
import numpy
import wx
import u3
import numpy as np
matplotlib.use('WXAgg')

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure


class TemperaturePanel( wx.Panel ) :
    def __init__( self, parent, position ) :
        wx.Panel.__init__( self, parent, pos=position, size=(800,320) )
        # initialize matplotlib 
        self.figure = matplotlib.figure.Figure( None, facecolor="white" )
        self.canvas = matplotlib.backends.backend_wxagg.FigureCanvasWxAgg( self, -1, self.figure )
        self.axes = self.figure.add_subplot(111)
        self.axes.grid(True, color="gray")
        self.axes.set_xbound( (0,5) )
        self.axes.set_ybound( (3,80) )
        self.axes.set_xlabel( "Minutes" )
        self.axes.set_ylabel( "Temperature ($^\circ$C)" )
        self.axes = self.figure.add_subplot(111)
        self.axes.grid(True, color="gray")
        self._SetSize()
        self.Bind( wx.EVT_SIZE, self._SetSize )
        self.TemperatureData   = []

    def updateTemperature(self, value):
        self.TemperatureData.append( value )
        length = len(self.TemperatureData)
        x = np.arange( length )
        y = np.array(self.TemperatureData)

        yMin = round(min(y)) - 2
        yMax = round(max(y)) + 2            
        self.axes.plot(x,y, "-k")
        self.axes.set_ybound( (yMin,yMax) )
        self.canvas = FigureCanvas(self, -1, self.figure)

    #-----------------------------------------------------------------------------------
    def _SetSize( self, event=None ):
        pixels = self.GetSize()
        self.SetSize( pixels )
        self.canvas.SetSize( pixels )

        dpi = self.figure.get_dpi()
        self.figure.set_size_inches( float( pixels[0] ) / dpi,float( pixels[1] ) / dpi )
    #------------------------------------------------------------------------------------




class MainWindow(wx.Frame):
    def __init__(self, parent):
        #wx.Frame.__init__(self, *args, **kwargs)
        wx.Frame.__init__(self, parent, title="Graph Issue", size=(1000,600))
        self.panel = wx.Panel(self)
        self.spin = wx.SpinCtrl(self.panel)
        self.button = wx.Button(self.panel, label="Update")
        self.stop   = wx.Button(self.panel, label="Stop")

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.spin)
        self.sizer.Add(self.button)
        self.sizer.Add(self.stop)

        self.TemperatureGraph = TemperaturePanel( self, position=(20, 50) )
        self.panel.SetSizerAndFit(self.sizer)
        self.Show()

        # Use EVT_CHAR_HOOK on Frame insted of wx.EVT_KEY_UP on SpinCtrl
        # to disable "on Enter go to next widget" functionality
        self.Bind(wx.EVT_CHAR_HOOK, self.OnKey) 
        self.button.Bind(wx.EVT_BUTTON, self.OnUpdate)
        self.stop.Bind(wx.EVT_BUTTON, self.OnStop)

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
        self.Bind(wx.EVT_TIMER, self.updateTemperature, self.timer)
        self.timer.Start(100)
        self.value = 0

    def OnKey(self, e):
        if e.GetKeyCode() == wx.WXK_RETURN:   # Is the key ENTER?
            self.value = self.spin.GetValue() # Read SpinCtrl and set internal value
        else:                                 # Else let the event out of the handler
            e.Skip()

    def OnUpdate(self, e):
        self.value = self.spin.GetValue() # Read SpinCtrl and set internal value

    def OnTimer(self, e):
        # Show internal value
        print(self.value)

    def updateTemperature(self, e):
        Temperature       = self.value*random.uniform(-1,1)                # obtain currnt temperature
        self.TemperatureGraph.updateTemperature(Temperature)               # add temperature to graph   

    def OnStop(self, e):
        self.timer.Stop()
        self.Destroy()


app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

如果我正确理解了这个问题,您只需要在图表中显示25个最后的温度值,而不是所有值的历史记录。如果这是您想要的,那么在
updateTemperature
子例程中,只应绘制最后25个值:

if length < 25:
    x = np.arange(length)
    y = np.array(self.TemperatureData)
else:
    x = np.arange(length-25, length)
    y = np.array(self.TemperatureData)[-25:]

我不明白你的问题。我建议你重写或编辑以“我的问题…”开头的段落。第一句话对我来说没有意义,下一句也是最后一句,“看起来不正确”并不是很有用。谢谢你,汤姆10-安德烈·索波列夫明白了
xMin = 0 if length < 25 else length-25
xMax = 25 if length < 25 else length
self.axes.set_xbound( (xMin,xMax) )
    self.canvas.draw()