python - canvas.restore_region() 不工作

标签 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 的半透明副本。 (如何)这可能吗?

最佳答案

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

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

关于python - canvas.restore_region() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29522691/

相关文章:

python - 使用 Tinymce 处理 django 错误。

python - 如何使用python追加多维数组?

python - 对与 basemap Python 绘图相对应的直方图进行子绘制

python - 如何随着时间的推移绘制信号的积分?

python - Seaborn 热图轴中的日期

python - 无法从github安装python库

python - WinAppDriver 和 Appium - send.keys() 发送 '#' 代替 ''\'

python-2.7 - Matplotlib:xticks 标签未显示

python - 为什么会出现两个而不是一个图形对象?

javascript - 将数据从 Python 发送到 Javascript (JSON)