Skip to main content
 首页 » 编程设计

python之用 python 和 matplotlib 绘制图形问题

2025年05月04日12lonelyxmas

我正在制作一个应用程序,在经过一些处理后绘制一个图形。这是在用户输入一些值并按下按钮后完成的。但是我没有绘制这个数字。下面是一个简化的代码。如果我直接绘制 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() 

请您参考如下方法:

我认为您错过了 Canvas 的重绘。绘制新图是不够的,还必须刷新绘图 Pane !在 Output_Panel_Fig 的绘制方法中的绘图命令后添加一个 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()