在WxPython面板中嵌入matplotlib图

时间:2021-05-17 07:11:35

How do I embed a matplotlib figure object inside a WxPython panel?

如何在WxPython面板中嵌入matplotlib图形对象?

I googled around and saw complicated examples involving interactive graphics and other extra stuff. Can anybody help with a minimal example?

我google了一下,看到了涉及交互式图形和其他额外内容的复杂示例。有人可以帮助一个最小的例子吗?

2 个解决方案

#1


34  

This is a minimal example for a Panel with a matplotlib canvas:

这是带有matplotlib画布的Panel的最小示例:

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 CanvasPanel(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)
        self.Fit()

    def draw(self):
        t = arange(0.0, 3.0, 0.01)
        s = sin(2 * pi * t)
        self.axes.plot(t, s)


if __name__ == "__main__":
    app = wx.PySimpleApp()
    fr = wx.Frame(None, title='test')
    panel = CanvasPanel(fr)
    panel.draw()
    fr.Show()
    app.MainLoop()

在WxPython面板中嵌入matplotlib图

#2


1  

Defining the frame size:

定义框架大小:

if __name__ == "__main__":
    app = wx.App()
    fr = wx.Frame(None, title='test', size=wx.Size(806, 450))
    panel = CanvasPanel(fr)
    panel.draw()
    fr.Show()
    app.MainLoop()

or defining the panel size:

或定义面板尺寸:

class CanvasPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent,size=wx.Size(806, 450))
...

#1


34  

This is a minimal example for a Panel with a matplotlib canvas:

这是带有matplotlib画布的Panel的最小示例:

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 CanvasPanel(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)
        self.Fit()

    def draw(self):
        t = arange(0.0, 3.0, 0.01)
        s = sin(2 * pi * t)
        self.axes.plot(t, s)


if __name__ == "__main__":
    app = wx.PySimpleApp()
    fr = wx.Frame(None, title='test')
    panel = CanvasPanel(fr)
    panel.draw()
    fr.Show()
    app.MainLoop()

在WxPython面板中嵌入matplotlib图

#2


1  

Defining the frame size:

定义框架大小:

if __name__ == "__main__":
    app = wx.App()
    fr = wx.Frame(None, title='test', size=wx.Size(806, 450))
    panel = CanvasPanel(fr)
    panel.draw()
    fr.Show()
    app.MainLoop()

or defining the panel size:

或定义面板尺寸:

class CanvasPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent,size=wx.Size(806, 450))
...