简体   繁体   English

matplotlib blitting(动画情节)无法调整图形大小

[英]matplotlib blitting (animated plot) can't resize figure

I have an animated line using the example given on the matplotlib site :我有一条动画线,使用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()

This yields the plot:这产生了 plot:

常规图

When I resize the window of the figure, it no longer works as intended:当我调整图中 window 的大小时,它不再按预期工作:

调整大小的图

What is the problem here?这里有什么问题?

I am having the same issue.我有同样的问题。 This seems to happen as you save the bitmap of the background the first frame and avoid redrawing it.这似乎发生在您将背景的 bitmap 保存为第一帧并避免重绘它时。 I found that this can be solved by checking for resizes and updating the background to the new one.我发现这可以通过检查调整大小并将背景更新为新背景来解决。 Your example fixed bellow:您的示例固定如下:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM