canvas.restore_region()不起作用

时间:2015-04-08 18:34:31

标签: python matplotlib

我试图按照以下说明操作: http://wiki.scipy.org/Cookbook/Matplotlib/Animations

为什么下面的代码没有显示预期的行为? (参见代码中关于预期内容与观察内容的评论)

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??

后续问题:

  • 我打算在相同轴上的修改位置上生成background的半透明副本。 (怎么样)这可能吗?

1 个答案:

答案 0 :(得分:3)

正如@tcaswell所说,你需要在draw()之前强制background = canvas.copy_from_bbox(ax.bbox)来电。尝试

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