wxpython中嵌入matplotlib图像

xiaohai 2020-10-17 22:33:31 1518人围观 标签: Python 
简介wxpython中嵌入matplotlib图像

wxpython中嵌入matplotlib图像

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

import numpy as np


class CanvasFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1,
                          'CanvasFrame', size=(550, 350))

        self.SetBackgroundColour('blue')

        ###############重要部分S###############
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)

        x = np.linspace(-4, 4, 30)
        y = np.sin(x)

        self.axes.plot(x, y, '--*b')

        self.canvas = FigureCanvas(self, -1, self.figure)
        ###############重要部分E###############

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)
        self.Fit()


class MyApp(wx.App):
    def OnInit(self):
        'Create the main window and insert the custom frame'
        frame = CanvasFrame()
        frame.Show(True)

        return True


app = MyApp()
app.MainLoop()