简体   繁体   中英

wxPython Refresh a plot with another graph by a button event

I set up a complete GUI and I already set up plots with no content. I have a list of parameters which are needed to calculate the plots content.

The graph should be plotted by hitting one button. plotting different graphs by changing the parameters should also be possible. So hitting the button and plotting the graph shouldn't be a one time event.

I used matplot.figurecanvas for the plots but any alternative is helpful, too.

I tried many different things so far, but nothing really works.

Can anyone help?

In your button event you have to clear the Figure, plot the new data and redraw the canvas.

Here's a simple example:

import wx
import matplotlib
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure

class MainFrame(wx.Frame): 
    def __init__(self): 
        wx.Frame.__init__(self, None, wx.NewId(), "Main") 
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.figure = Figure(figsize=(1,2))
        self.axe = self.figure.add_subplot(111)
        self.figurecanvas = FigureCanvas(self, -1, self.figure)
        self.valueCtrl = wx.TextCtrl(self, -1, "")
        self.buttonPlot = wx.Button(self, wx.NewId(), "Plot")
        self.sizer.Add(self.figurecanvas, proportion=1, border=5,
                       flag=wx.ALL | wx.EXPAND)
        self.sizer.Add(self.buttonPlot, proportion=0, border=2, flag=wx.ALL)
        self.sizer.Add(self.valueCtrl, proportion=0, border=2, flag=wx.ALL)
        self.SetSizer(self.sizer)
        self.buttonPlot.Bind(wx.EVT_BUTTON, self.on_button_plot)

    def on_button_plot(self, evt):
        self.figure.set_canvas(self.figurecanvas)
        self.axe.clear()
        self.axe.plot(range(int(self.valueCtrl.GetValue())), color='green')
        self.figurecanvas.draw()

class MyApp(wx.App):
    def OnInit(self):
        frame = MainFrame()
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

app = MyApp(0)
app.MainLoop() 

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM