【问题标题】:WXPython with MatPlotLibWXPython 与 MatPlotLib
【发布时间】:2013-11-11 02:55:00
【问题描述】:

我正在尝试将 MatPlotLib 与 WXPython 一起使用。我从http://www.cs.colorado.edu/~kena/classes/5448/s11/presentations/pearse.pdf 中找到了一个很好的例子 因为它展示了如何使用 2 个面板,并且很好地解释了如何嵌入 matplotlib。但我认为它缺少某些部分,因为图形没有被初始化。 我该如何解决?

import wx
import numpy
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

class TestFrame(wx.Frame):
    def __init__(self,parent,title):
        wx.Frame.__init__(self,parent,title=title,size=(500,500))
        self.sp = wx.SplitterWindow(self)
        self.p1 = wx.Panel(self.sp, style=wx.SUNKEN_BORDER)
        self.p2 = wx.Panel(self.sp, style=wx.SUNKEN_BORDER)
        self.sp.SplitVertically(self.p1,self.p2,100)
        self.statusbar = self.CreateStatusBar()
        self.statusbar.SetStatusText('Oi')

class p1(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent,-1,size=(50,50))

        self.figure = matplotlib.figure.Figure()
        self.axes = self.figure.add_subplot(111)
        t = numpy.arange(0.0,10,1.0)
        s = [0,1,0,1,0,2,1,2,1,0]
        self.y_max = 1.0
        self.axes,plot(t,s)
        self.canvas = FigureCanvas(self,-1,self.figure)

app = wx.App(redirect=False)
frame = TestFrame(None, 'Hello World!')
frame.Show()
app.MainLoop()

谢谢。

【问题讨论】:

    标签: python numpy matplotlib wxpython


    【解决方案1】:

    您使用matplot 创建了p1 类,但您没有在TestFrame 中使用它。

    我更改了一些名称以使其更清晰

    import wx
    import numpy 
    import matplotlib
    
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
    
    class TestFrame(wx.Frame):
        def __init__(self,parent,title):
            wx.Frame.__init__(self,parent,title=title,size=(500,500))
            self.sp = wx.SplitterWindow(self)
            self.p1 = wx.Panel(self.sp, style=wx.SUNKEN_BORDER)
            self.p2 = MatplotPanel(self.sp)
            self.sp.SplitVertically(self.p1,self.p2,100)
            self.statusbar = self.CreateStatusBar()
            self.statusbar.SetStatusText('Oi')
    
    class MatplotPanel(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent,-1,size=(50,50))
    
            self.figure = Figure()
            self.axes = self.figure.add_subplot(111)
            t = numpy.arange(0.0,10,1.0)
            s = [0,1,0,1,0,2,1,2,1,0]
            self.y_max = 1.0
            self.axes.plot(t,s)
            self.canvas = FigureCanvas(self,-1,self.figure)
    
    app = wx.App(redirect=False)
    frame = TestFrame(None, 'Hello World!')
    frame.Show()
    app.MainLoop()
    

    另见(例如)我对how to combine wxPython, matplotlib and Pyopengl的回答

    编辑:

    MatplotPanelNavigationToolbarwx.Button 更改情节。

    编辑:2022.01.03

    正如@Samuel 在评论中所说:较新的matplotlib 需要NavigationToolbar2WxAgg 而不是NavigationToolbar2Wx。另见matplotlib 文档:Embedding in wx #2

    # older matplotlib
    #from matplotlib.backends.backend_wxagg import NavigationToolbar2Wx as NavigationToolbar
    
    # newer matplotlib 
    from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar
    
    class MatplotPanel(wx.Panel):
        
        def __init__(self, parent):     
            wx.Panel.__init__(self, parent,-1,size=(50,50))
    
            self.sizer = wx.BoxSizer(wx.VERTICAL)
            self.SetSizer(self.sizer)
            
            self.figure = Figure()
            self.axes = self.figure.add_subplot(111)
            self.canvas = FigureCanvas(self, -1, self.figure)
            self.toolbar = NavigationToolbar(self.canvas)
    
            self.button = wx.Button(self, -1, "Change plot")
            self.button.Bind(wx.EVT_BUTTON, self.changePlot)
            
            self.sizer.Add(self.toolbar, 0, wx.EXPAND)
            self.sizer.Add(self.button, 0, wx.EXPAND)
            self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
            
            self.drawSin()
            self.current_draw = 'sin'
            
    #       self.drawLines()
    
        def changePlot(self, event):
            
            if self.current_draw == 'sin' :
                self.drawLines()
                self.current_draw = 'lines'
            else: 
                self.drawSin()
                self.current_draw = 'sin'
                
            self.Layout()
            
        def drawLines(self):
        
            x = numpy.arange(0.0,10,1.0)
            y = [0,1,0,1,0,2,1,2,1,0]
            
            self.axes.clear()
            self.axes.plot(x, y)
    
        def drawSin(self):
        
            x = numpy.arange(0.0,10,0.1)
            y = numpy.sin(x)
            
            self.axes.clear()
            self.axes.plot(x, y)
    

    【讨论】:

    • 谢谢@furas。我是 Matplotlib 和 WxPython 的新手。如何在此程序中放置带有 Home/Subplot/Zoom 等的框?通常是自动的。
    • 我添加了带有导航工具栏的示例。
    • 我添加wx.Button 来改变情节
    • 哇,很好,@furas。但对我来说,按钮有问题。当我单击时没有任何反应,但有时当我首先单击 Pan 按钮时,它会变为 line(但反之亦然)。
    • 在导入行将“Agg”添加到“NavigationToolbar2Wx”以修复新版本的 matplotlib 的错误。
    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 2011-09-23
    • 2012-06-21
    • 2023-03-17
    • 1970-01-01
    • 2023-03-21
    • 2013-11-22
    • 2011-01-12
    相关资源
    最近更新 更多