BerndGit
BerndGit

Reputation: 1658

canvas.restore_region() not working

I was trying to follow the instructions on: http://wiki.scipy.org/Cookbook/Matplotlib/Animations

Why is the code below not showing the expected behaviour? (see comments in code on what is expected vs. what is observed)

import matplotlib.pyplot as plt
from pylab import rand

f, ax = plt.subplots()

# Generate dummy plot
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])
ax.imshow(rand(10,10),  interpolation='nearest')
# Until here it works
# If i would add a 
#     f.show() 
# i would see the image

# Save figure
canvas = ax.figure.canvas
background = canvas.copy_from_bbox(ax.bbox)  

# Clear Axes
ax.cla()

# try to restore figure
canvas.restore_region(background)
f.show()

# Unfortunatly I only see an empty figure now, Why??

Follow-up question:

Upvotes: 3

Views: 3039

Answers (1)

jorgeh
jorgeh

Reputation: 1767

As @tcaswell said, you need to force a draw() call begore background = canvas.copy_from_bbox(ax.bbox). Try

...
canvas = ax.figure.canvas
canvas.draw()
background = canvas.copy_from_bbox(ax.bbox)  
...

Upvotes: 3

Related Questions