[英]Draggable Matplotlib scatter with an animated plot using blitting
[英]matplotlib blitting (animated plot) can't resize figure
我有一条动画线,使用matplotlib 网站上给出的示例:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
fig, ax = plt.subplots()
(ln,) = ax.plot(x, np.sin(x), animated=True)
plt.show(block=False)
plt.pause(0.1)
bg = fig.canvas.copy_from_bbox(fig.bbox)
ax.draw_artist(ln)
fig.canvas.blit(fig.bbox)
for j in range(100):
fig.canvas.restore_region(bg)
ln.set_ydata(np.sin(x + (j / 100) * np.pi))
ax.draw_artist(ln)
fig.canvas.blit(fig.bbox)
fig.canvas.flush_events()
这产生了 plot:
当我调整图中 window 的大小时,它不再按预期工作:
这里有什么问题?
我有同样的问题。 这似乎发生在您将背景的 bitmap 保存为第一帧并避免重绘它时。 我发现这可以通过检查调整大小并将背景更新为新背景来解决。 您的示例固定如下:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
fig, ax = plt.subplots()
(ln,) = ax.plot(x, np.sin(x), animated=True)
plt.show(block=False)
plt.pause(0.1)
bg = fig.canvas.copy_from_bbox(fig.bbox)
ax.draw_artist(ln)
fig.canvas.blit(fig.bbox)
old_fig_size = fig.get_size_inches()
for j in range(100):
if (old_fig_size != fig.get_size_inches()).any():
bg = fig.canvas.copy_from_bbox(fig.bbox)
old_fig_size = fig.get_size_inches()
fig.canvas.restore_region(bg)
ln.set_ydata(np.sin(x + (j / 100) * np.pi))
ax.draw_artist(ln)
fig.canvas.blit(fig.bbox)
fig.canvas.flush_events()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.