matplotlib panel resize troubles

Hello,

I'm trying to embed matplotlib figure in wxpython application but I
have troubles when resizing. If my windows become to big, a part of
the plot does not appear.
A screenshot : http://tonyducrocq.free.fr/err_matplotlib.png
Here is the code of panel :

#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx

from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.font_manager import FontProperties
import numpy

PLOT_FONT_SIZE = 9

class PlotsPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
  self.fig = Figure()
        self.canvas = FigureCanvasWxAgg(self, -1, self.fig)
  self.Bind(wx.EVT_SIZE, self.OnSize)
  self.parent = parent
  
    def OnSize(self, event):
        size = self.parent.GetClientSize()
        self.fig.set_figwidth(size[0]/(1.0*self.fig.get_dpi()))
  self.fig.set_figheight(size[1]/(1.0*self.fig.get_dpi()))
  self.canvas.resize(size[0],size[1])
  self.canvas.draw()
        event.Skip()

class IdentityPanel(PlotsPanel):
    def plotMatrix(self,mat):
  self.mat = mat
        self.fig.clf()
        a = self.fig.add_subplot(111)
        self.im = a.imshow(mat, origin='lower', interpolation='nearest')
        self.fig.colorbar(self.im)
        self.canvas.draw()

    def onEraseBackground(self, evt):
        # this is supposed to prevent redraw flicker on some X servers...
        pass

Thanks by advance

···

--
Tony Ducrocq

Hi Tony,

I'm trying to embed matplotlib figure in wxpython application but I
have troubles when resizing. If my windows become to big, a part of
the plot does not appear.
A screenshot : http://tonyducrocq.free.fr/err_matplotlib.png
Here is the code of panel :

#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx

from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.font_manager import FontProperties
import numpy

PLOT_FONT_SIZE = 9

class PlotsPanel(wx.Panel):
   def __init__(self, parent):
       wx.Panel.__init__(self, parent, -1)
       self.fig = Figure()
       self.canvas = FigureCanvasWxAgg(self, -1, self.fig)
       self.Bind(wx.EVT_SIZE, self.OnSize)
       self.parent = parent

   def OnSize(self, event):
       size = self.parent.GetClientSize()
       self.fig.set_figwidth(size[0]/(1.0*self.fig.get_dpi()))
       self.fig.set_figheight(size[1]/(1.0*self.fig.get_dpi()))
       self.canvas.resize(size[0],size[1])
       self.canvas.draw()
       event.Skip()

Does anything change if you modify your OnSize handler like this:

    def OnSize(self, event):

        event.Skip()
        wx.CallAfter(self.ResizeCanvas)

    def ResizeCanvas(self):

        size = self.parent.GetClientSize()
        self.fig.set_figwidth(size[0]/(1.0*self.fig.get_dpi()))
        self.fig.set_figheight(size[1]/(1.0*self.fig.get_dpi()))
        self.canvas.resize(size[0],size[1])
        self.canvas.draw()

? If it doesn't, you may want to look at the actual values of size[0],
size[1] and the matplotlib figure width and height.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

···

On 8/20/08, Tony Ducrocq wrote:

Hi Andrea,

your solution didn't work but I have investigate by printing sizes of
the panel, figure and canvas and I have discover that the size of the
canvas was always the same so I've trie other resize function and
SetClientSize() was the good function.

Thank you very much, it work's like a charm :slight_smile:

···

On Wed, Aug 20, 2008 at 12:35 PM, Andrea Gavana <andrea.gavana@gmail.com> wrote:

Hi Tony,

On 8/20/08, Tony Ducrocq wrote:

I'm trying to embed matplotlib figure in wxpython application but I
have troubles when resizing. If my windows become to big, a part of
the plot does not appear.
A screenshot : http://tonyducrocq.free.fr/err_matplotlib.png
Here is the code of panel :

#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx

from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.font_manager import FontProperties
import numpy

PLOT_FONT_SIZE = 9

class PlotsPanel(wx.Panel):
   def __init__(self, parent):
       wx.Panel.__init__(self, parent, -1)
       self.fig = Figure()
       self.canvas = FigureCanvasWxAgg(self, -1, self.fig)
       self.Bind(wx.EVT_SIZE, self.OnSize)
       self.parent = parent

   def OnSize(self, event):
       size = self.parent.GetClientSize()
       self.fig.set_figwidth(size[0]/(1.0*self.fig.get_dpi()))
       self.fig.set_figheight(size[1]/(1.0*self.fig.get_dpi()))
       self.canvas.resize(size[0],size[1])
       self.canvas.draw()
       event.Skip()

Does anything change if you modify your OnSize handler like this:

   def OnSize(self, event):

       event.Skip()
       wx.CallAfter(self.ResizeCanvas)

   def ResizeCanvas(self):

       size = self.parent.GetClientSize()
       self.fig.set_figwidth(size[0]/(1.0*self.fig.get_dpi()))
       self.fig.set_figheight(size[1]/(1.0*self.fig.get_dpi()))
       self.canvas.resize(size[0],size[1])
       self.canvas.draw()

? If it doesn't, you may want to look at the actual values of size[0],
size[1] and the matplotlib figure width and height.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

--
Tony Ducrocq