[matplotlib-devel] Bug with restore_background when mpl is embedded in wxpython

2009-05-09 Thread Elan Pavlov
Hi,
Restore_region does not appear to work when embedding mpl into
wxpython. Attached is a simple modification of the cookbook animation
(http://www.scipy.org/Cookbook/Matplotlib/Animations) to illustrate
this problem. I modified the example so it is updated on mouse
movements (followed by idle time) so that the problem is more visual.
There is also a flag to add one second timeouts between commands in
the update method. When looking at the slowed animation it is clear
that the background is never restored.
Michiel Hoon thinks that the problem is due to the fact that
restore_region is not implemented within the event loop and that the
proper solution requires discussion:)

Elan
---
Of joys departed, not to return, how painful the remembrance.
- Robert Blair
#!/usr/bin/env python

import matplotlib
matplotlib.use('WXAgg')
import matplotlib.numerix as nx

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas
import wx
import time

# A flag to pause between every command in the update stage
slowdown=False
#--
class MainFrame(wx.Frame,matplotlib.figure.Figure):
"""Main frame for a simple graph."""
def __init__(self, parent, id):
wx.Frame.__init__(self,None,-1)

# creating the matplotlib figure and canvas.
self._fig = matplotlib.figure.Figure( None)
self.canvas = FigCanvas(self, wx.ID_ANY, self._fig )


# creating some data
self.x = nx.arange(0,2*nx.pi,0.01)


# adding the suplot and a line.
self.ax = self._fig.add_subplot(111)
self.line, = self.ax.plot(self.x, nx.sin(self.x), animated=True)
if slowdown==True:
time.sleep(1)

# save the clean slate background -- everything but the animated line
# is drawn and saved in the pixel buffer background
self.background = self._fig.canvas.copy_from_bbox(self.ax.bbox)


# Update when there is idle time. Note that this will only refire when 
# something happens (such as a mouse movement) this will allow
# a better view of the dynamics of the updates.
self.Bind(wx.EVT_IDLE, self.update_line)

# a variable to induce change
self.update_line_cnt=0



return
#--
def update_line(self,event):
""" This function will update and redraw the data"""

# update the data
self.line.set_ydata(nx.sin(self.x+self.update_line_cnt/20.0))

# restore the clean slate background
self._fig.canvas.restore_region(self.background)
if slowdown==True:
time.sleep(1)

# just draw the animated artist
self.ax.draw_artist(self.line)
if slowdown==True:
time.sleep(1)

# just redraw the axes rectangle
self._fig.canvas.blit(self.ax.bbox)
if slowdown==True:
time.sleep(1)

# Uncomment to speed up updates without actions.
#event.RequestMore(True)

#Getting some changes
self.update_line_cnt+=1

return True

#--
if __name__ == '__main__':

App=wx.App() 
frame=MainFrame(None,-1)
frame.Show(True)
App.MainLoop()  







--
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image 
processing features enabled. http://p.sf.net/sfu/kodak-com___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel


Re: [matplotlib-devel] Bug with restore_background when mpl is embedded in wxpython

2009-05-10 Thread Elan Pavlov
Hi John and Jae-Joon,
While it hardly solves *all* of the issues I've been having it does work 
for this problem:) Thanks a ton. As for committing it to the repository 
I'd be honored if Jae Joon agrees. Should I clean it up and remove the 
debugging prints?

Elan

"If stupidity got us into this mess, why can't it get us out?"
- Will Rogers

On Sat, 9 May 2009, John Hunter wrote:

> On Sat, May 9, 2009 at 12:08 PM, Jae-Joon Lee  wrote:
>> Here is a slightly modified version of your script which works for me.
>>
>> I don't think this is the bug in mpl. Note that the ax.bbox does
>> change if the canvas size change. In your original script, the
>> copy_from_bbox is called before frame.Show() and this seems to cause a
>> mismatching bbox.
>>
>> My modified example is also not perfect. A new background image needs
>> to be saved whenever ax.bbox changes (e.g. when resizing canvas).
>
> probably  best way to do this is to connect to the draw_event, and
> update the background on the draw_event.  I've updated the example and
> attached it -- do you mind if I commit it to the mpl examples dir (and
> does this solve all the issues you are having)?
>
> JDH
>

--
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image 
processing features enabled. http://p.sf.net/sfu/kodak-com
___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel