简体   繁体   English

用python和matplotlib绘制图形问题

[英]Plot figure problems with python and matplotlib

I'm making an app that plots a figure after some processing. 我正在制作一个应用程序,该应用程序经过一些处理后可以绘制图形。 This is done after the user has introduced some values and pushes a button. 这是在用户引入一些值并按下按钮之后完成的。 However I don't get the figure plotted. 但是我没有得到这个数字。 Below there is a simplified code. 下面有一个简化的代码。 This works fine if I plot directly the values of t and s, but not if it is done after pushing the button. 如果我直接绘制t和s的值,则效果很好,但如果在按下按钮后完成,则效果不佳。 What am I missing? 我想念什么? Is there another better way to do so? 还有其他更好的方法吗?

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()

I think you are missing a redraw of the canvas. 我认为您错过了画布的重画。 It is not enough to do a new plot but a refresh of the drawing pane must be done! 仅仅做一个新的绘图是不够的,但是必须刷新绘图窗格! Add a self.canvas.draw() after your plot command in the draw method of the Output_Panel_Fig this should help. 在Output_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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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