在wxPython中嵌入子图

在wxPython中嵌入子图,wxpython,matplotlib,python-2.5,Wxpython,Matplotlib,Python 2.5,我使用的是Python活动版本2.5(32位)。我在wxPython中进行子绘图时遇到了一个问题。在程序中,当我单击第一个按钮时,两个子批次工作正常,但当我单击第二个按钮时,重叠发生在第一个子批次中,而第二个子批次根据程序工作正常。此外,当我再次单击第一个按钮时,第一个子批次不会更改,而第二个子批次会根据程序进行更改。您可以在运行程序时观察它。我只想消除重叠问题和第一个子地块的静态特性,即再次单击第1或第2按钮,第一个子地块应该像第二个子地块一样改变 还有一件事:如何根据日期绘制数据?Pytho

我使用的是Python活动版本2.5(32位)。我在wxPython中进行子绘图时遇到了一个问题。在程序中,当我单击第一个按钮时,两个子批次工作正常,但当我单击第二个按钮时,重叠发生在第一个子批次中,而第二个子批次根据程序工作正常。此外,当我再次单击第一个按钮时,第一个子批次不会更改,而第二个子批次会根据程序进行更改。您可以在运行程序时观察它。我只想消除重叠问题和第一个子地块的静态特性,即再次单击第1或第2按钮,第一个子地块应该像第二个子地块一样改变

还有一件事:如何根据日期绘制数据?Python给出了一个错误,因为Python没有将日期转换为浮点,而对于绘图,我认为有必要将数据转换为浮点

等待积极的回应

    import sys,os,csv
    import numpy as N
    import wx
    import matplotlib
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_wxagg import \
        FigureCanvasWxAgg as FigCanvas, \
        NavigationToolbar2WxAgg as NavigationToolbar
    class FinalProject(wx.Frame):
        title = ' FYP Project '
        def __init__(self):
            wx.Frame.__init__(self, None, -1, self.title)
            self.create_menu()
            self.create_status_bar()
            self.create_main_panel()
        def create_menu(self):
            self.menubar = wx.MenuBar()
            menu_file = wx.Menu()
            m_expt = menu_file.Append(-1, "&Save plot\tCtrl-S", "Save plot to file")
            self.Bind(wx.EVT_MENU, self.on_save_plot, m_expt)
            menu_file.AppendSeparator()
            m_exit = menu_file.Append(-1, "E&xit\tCtrl-X", "Exit")
            self.Bind(wx.EVT_MENU, self.on_exit, m_exit)
            self.menubar.Append(menu_file, "&File")
            self.SetMenuBar(self.menubar)
        def create_main_panel(self):
            self.panel = wx.Panel(self)
            self.dpi = 100
            self.fig = Figure((9.5, 5.0), dpi=self.dpi)
            self.canvas = FigCanvas(self.panel, -1, self.fig)
            self.axes = self.fig.add_subplot(1,2,1)
            self.axes = self.fig.add_subplot(1,2,2)
            self.drawbutton1 = wx.Button(self.panel, -1, "Trial Version 1")
            self.Bind(wx.EVT_BUTTON, self.on_draw_button1, self.drawbutton1)
            self.drawbutton2 = wx.Button(self.panel, -1, "Trial Version 2")
            self.Bind(wx.EVT_BUTTON, self.on_draw_button2, self.drawbutton2)
            self.toolbar = NavigationToolbar(self.canvas)
            self.vbox = wx.BoxSizer(wx.VERTICAL)
            self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
            self.vbox.Add(self.toolbar, 0, wx.EXPAND)
            self.vbox.AddSpacer(10)
            self.hbox = wx.BoxSizer(wx.HORIZONTAL)
            flags = wx.ALIGN_LEFT | wx.ALL | wx.ALIGN_CENTER_VERTICAL
            self.hbox.Add(self.drawbutton1, 0, border=3, flag=flags)
            self.hbox.Add(self.drawbutton2, 0, border=3, flag=flags)
            self.vbox.Add(self.hbox, 0, flag = wx.ALIGN_LEFT | wx.TOP)
            self.panel.SetSizer(self.vbox)
            self.vbox.Fit(self)
        def create_status_bar(self):
            self.statusbar = self.CreateStatusBar()
        def on_draw_button1(self, event):
            self.axes.clear()
            i = N.arange(0,4,1)
            q = i
            w = N.arange(-4,0,1)
            self.axes = self.fig.add_subplot(1,2,1)
            self.axes.plot(q,i,'red')
            self.axes = self.fig.add_subplot(1,2,2)
            self.axes.plot(w,i,'yellow')
            self.canvas.draw()
        def on_draw_button2(self, event):
            self.axes.clear()
            a = [0,1,2,3,4,]
            b = [5.5,4.5,3.5,2.5,1.5]
            c = [7.5,2.5,4,6.8,10.6]
            self.axes = self.fig.add_subplot(1,2,1)
            self.axes.plot(b,a,'purple')
            self.axes = self.fig.add_subplot(1,2,2)
            self.axes.plot(c,a,'black')
            self.canvas.draw()
        def on_save_plot(self, event): 
            file_choices = "PNG (*.png)|*.png"

            dlg = wx.FileDialog(
                self, 
                message="Save plot as...",
                defaultDir=os.getcwd(),
                defaultFile="plot.png",
                wildcard=file_choices,
                style=wx.SAVE)

            if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                self.canvas.print_figure(path, dpi=self.dpi)
                self.flash_status_message("Saved to %s" % path)

        def on_exit(self, event):
            self.Destroy()
    if __name__ == '__main__':
        app = wx.PySimpleApp()
        app.frame = FinalProject()
        app.frame.Show()
        app.MainLoop()
        del app

您的代码需要一些修复。您需要区分正在使用的两个轴。您不需要每次都创建它们,只需清除它们,然后在相应的轴上绘制每条线

以下更改使代码按预期工作:

def create_main_panel(self):
    self.panel = wx.Panel(self)
    self.dpi = 100
    self.fig = Figure((9.5, 5.0), dpi=self.dpi)
    self.canvas = FigCanvas(self.panel, -1, self.fig)
    self.axes1 = self.fig.add_subplot(1,2,1)            # <--
    self.axes2 = self.fig.add_subplot(1,2,2)            # <--
    self.drawbutton1 = wx.Button(self.panel, -1, "Trial Version 1")
    ............................

def on_draw_button1(self, event):
    self.axes1.clear()                           # <--
    self.axes2.clear()                           # <--
    i = N.arange(0,4,1)
    q = i
    w = N.arange(-4,0,1)
    #self.axes = self.fig.add_subplot(1,2,1)     # <-- delete
    self.axes1.plot(q,i,'red')                   # <--
    #self.axes = self.fig.add_subplot(1,2,2)     # <-- delete
    self.axes2.plot(w,i,'yellow')                # <--
    self.canvas.draw()
def on_draw_button2(self, event):
    self.axes1.clear()                           # <--
    self.axes2.clear()                           # <--
    a = [0,1,2,3,4,]
    b = [5.5,4.5,3.5,2.5,1.5]
    c = [7.5,2.5,4,6.8,10.6]
    #self.axes = self.fig.add_subplot(1,2,1)     # <-- delete
    self.axes1.plot(b,a,'purple')                # <--
    #self.axes = self.fig.add_subplot(1,2,2)     # <-- delete
    self.axes2.plot(c,a,'black')                 # <--
    self.canvas.draw()
def创建主面板(自):
self.panel=wx.panel(self)
self.dpi=100
self.fig=图((9.5,5.0),dpi=self.dpi)
self.canvas=FigCanvas(self.panel,-1,self.fig)
self.axes1=self.fig.add_子批次(1,2,1)#