Search code examples
pythonmatplotlibwxpython

embedding matplotlib figure and change its size


I had the same problem like here.
It works, but this figure shall not cover the whole frame; so I changed size and position of his panel. Problem now is, that my figure still has the same size of my frame and therefore I only changed the panelsize, my figure gets "cut". How can I solve this?
Example for better understanding: My Frame is 800x600 and my figure shall only have the size 400x300 inside this frame.

edit:

import wx
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

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, 
                          "Pyramid App",size=(800,600),pos=((wx.DisplaySize()[0]-800)/2,(wx.DisplaySize()[1]-600)/2),style= wx.SYSTEM_MENU | wx.CAPTION | wx.MINIMIZE_BOX | wx.CLOSE_BOX)

        self.PageThree = pageThree(self)

class pageThree(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent,size=(800,525))
        self.myparent=parent
        self.pageThree=wx.Panel(self,size=(800,525)) # put some elements at this panel
        self.pylabfigure = wx.Panel(self,size=(440,420),pos=(350,105)) # this is the panel for my figure
        self.drawPylabFigure()
        self.draw()

    def drawPylabFigure(self):
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self.pylabfigure, -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):
        #example drawing
        t = arange(0.0, 3.0, 0.01)
        s = sin(2 * pi * t)
        self.axes.plot(t, s)

if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()

Solution

  • This will give you an idea of how to do it. Main thing to have into account it to put your panels/canvas organized by one sizer. Also, if the canvas has to fill his side there is not need to put it in a panel.

    import wx
    from numpy import arange, sin, pi
    import matplotlib
    from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FCW
    from matplotlib.figure import Figure
    
    class MainFrame(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY,
                              "Pyramid App",size=(800,600),
                               pos=((wx.DisplaySize()[0]-800)/2,(wx.DisplaySize()[1]-600)/2),
                               style=wx.SYSTEM_MENU | wx.CAPTION | wx.MINIMIZE_BOX | wx.CLOSE_BOX)
    
            self.PageThree = pageThree(self)
    
    class pageThree(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent=parent,size=(800,525))
            self.myparent=parent
            self.pageThree=wx.Panel(self, size=(500,525))
            self.drawPyFigure()
            self.draw()
    
        def drawPyFigure(self):
            self.figure = Figure()
            self.axes = self.figure.add_subplot(111)
            self.canvas = FCW(self, -1, self.figure)
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.Add(self.pageThree, 0)
            sizer.Add(self.canvas, 1, wx.EXPAND)
            self.SetSizer(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.App()
        MainFrame().Show()
        app.MainLoop()
    

    enter image description here