我正在使用Python Active版本2.5(32位)。我在wxPython中进行子绘图时遇到了问题。在程序中,当我点击第一个按钮时,两个子图工作正常,但是当我点击第二个按钮时,重叠发生在第一个子图,而第二个子图根据程序运行良好。此外,当我再次点击第一个按钮时,第一个子图不会改变,而第二个子图根据程序改变。您可以在运行程序时观察它。我想要的是删除重叠问题和第一个子图的静态性质,即再次点击第一个或第二个按钮,第一个子图应该像第二个子图一样改变。
还有一件事:如何根据日期绘制数据? 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
答案 0 :(得分:0)
您的代码需要一些修复。您需要区分正在使用的两个轴。您不需要每次都创建它们,只是为了清除它们,然后在相应的轴上绘制每一条线。
以下更改使您的代码按预期工作:
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()