我正在使用 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