Matplotlib bar chart in a wx Frame instead of a new window

后端 未结 1 1074
傲寒
傲寒 2021-01-17 05:05

I have a simple wxFrame and two panels in it. i want one of the panels to show a matplotlib bar chart. I learnt to use the chart, but the show() function that I use gives me

相关标签:
1条回答
  • 2021-01-17 05:38

    The pyplot functions you are using (p.figure) have the primary purpose of taking care of all the GUI details (which works at cross purposes to what you want). What you want to to is embed matplotlib in your pre-existing gui. For how to do this, see these examples.

    This code is pulled from embedding_in_wx2.py to protect against link-rot:

    #!/usr/bin/env python
    """
    An example of how to use wx or wxagg in an application with the new
    toolbar - comment out the setA_toolbar line for no toolbar
    """
    # Used to guarantee to use at least Wx2.8
    import wxversion
    wxversion.ensureMinimal('2.8')
    from numpy import arange, sin, pi
    
    import matplotlib
    
    # uncomment the following to use wx rather than wxagg
    #matplotlib.use('WX')
    #from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas
    
    # comment out the following to use wx rather than wxagg
    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 CanvasFrame(wx.Frame):
    
        def __init__(self):
            wx.Frame.__init__(self,None,-1,
                             'CanvasFrame',size=(550,350))
    
            self.SetBackgroundColour(wx.NamedColour("WHITE"))
            self.figure = Figure()
            self.axes = self.figure.add_subplot(111)
            t = arange(0.0,3.0,0.01)
            s = sin(2*pi*t)
            self.axes.plot(t,s)
            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 OnPaint(self, event):
            self.canvas.draw()
    
    class App(wx.App):
        def OnInit(self):
            'Create the main window and insert the custom frame'
            frame = CanvasFrame()
            frame.Show(True)
            return True
    
    app = App(0)
    app.MainLoop()
    
    0 讨论(0)
提交回复
热议问题