用python和matplotlib绘制图形问题

时间:2013-10-18 10:08:23

标签: python matplotlib plot wxpython

我正在制作一个应用程序,在经过一些处理之后绘制一个数字。这是在用户引入了一些值并按下按钮后完成的。但是我没有得到这个数字。下面是一个简化的代码。如果我直接绘制t和s的值,这可以正常工作,但如果在按下按钮后完成则不行。我错过了什么?还有另一种更好的方法吗?

from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WXAgg')

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

import wx

class Input_Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        # Input variables
        self.button = wx.Button(self, label="Go")

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(1, 1)
        self.sizer.Add(self.button, (1, 2), (3, 6), flag=wx.EXPAND)
        self.SetSizer(self.sizer)

class Output_Panel_Var(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        # Output variables
        self.tittle = wx.StaticText(self, label="OUTPUTS:")
        self.font = wx.Font(12, wx.DECORATIVE, wx.BOLD, wx.NORMAL)
        self.tittle.SetFont(self.font)
        self.lblt = wx.StaticText(self, label="t:")
        self.resultt = wx.StaticText(self, label="", size=(100, -1))
        self.lbls = wx.StaticText(self, label="s:")
        self.results = wx.StaticText(self, label="", size=(100, -1))

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(2, 2)
        self.sizer.Add(self.tittle, (1, 3))
        self.sizer.Add(self.lblt, (3, 1))
        self.sizer.Add(self.resultt, (3, 2))
        self.sizer.Add(self.lbls, (4, 1))
        self.sizer.Add(self.results, (4, 2))
        self.SetSizer(self.sizer)

class Output_Panel_Fig(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)

    def draw(self,t,s):
        self.axes.plot(t, s)


class Main_Window(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title = title, pos = (0, 0), size = wx.DisplaySize())

        # Set variable panels
        self.main_splitter = wx.SplitterWindow(self)
        self.out_splitter = wx.SplitterWindow(self.main_splitter)
        self.inputpanel = Input_Panel(self.main_splitter)
        self.inputpanel.SetBackgroundColour('#c4c4ff')
        self.outputpanelvar = Output_Panel_Var(self.out_splitter)
        self.outputpanelvar.SetBackgroundColour('#c2f1f5')
        self.outputpanelfig = Output_Panel_Fig(self.out_splitter)
        self.main_splitter.SplitVertically(self.inputpanel, self.out_splitter)
        self.out_splitter.SplitHorizontally(self.outputpanelvar, self.outputpanelfig)

        # Set event handlers
        self.inputpanel.button.Bind(wx.EVT_BUTTON, self.OnButton)      

    def OnButton(self, e):
        t = arange(0.0, 1.0, 0.01)
        s = sin(2 * pi * t)
        #self.outputpanelvar.resultt.SetLabel('%.5f' % t)
        #self.outputpanelvar.resultt.SetLabel('%.5f' % s)
        self.outputpanelfig.draw(t,s)

def main():
    app = wx.App(False)
    frame = Main_Window(None, "T-Matrix Codes GUI")
    frame.Show()
    app.MainLoop()
if __name__ == "__main__" :
    main()

1 个答案:

答案 0 :(得分:1)

我认为你错过了画布的重绘。仅创建新绘图是不够的,但必须刷新绘图窗格!在plot_Panel_Fig的draw方法中,在plot命令之后添加一个self.canvas.draw(),这应该有所帮助。

import ...

class Input_Panel(wx.Panel):
    def __init__(self, parent):
        ...

class Output_Panel_Var(wx.Panel):
    def __init__(self, parent):
        ...

class Output_Panel_Fig(wx.Panel):
    def __init__(self, parent):
        ...

    def draw(self,t,s):
        self.axes.plot(t, s)

        self.canvas.draw()


class Main_Window(wx.Frame):
    def __init__(self, parent, title):
        ...   

    def OnButton(self, e):
        ...

def main():
    ...

if __name__ == "__main__" :
    main()