用户保证:动画被删除而没有渲染任何内容

发布于 2025-02-08 06:41:33 字数 1287 浏览 2 评论 0原文

注意:只有最后一个代码块带来了错误。较早的块是为我想要的动画提供背景。这一切都在Windows的Jupyter中。

我有一个带有两个线段的matplotlib Pyplot,一个是“错误的”,另一个是“正确的”。我想将图形动画起来,以从两行开始,蓝色“错误”行是,并具有红色的一个枢轴并移动到正确的位置。 “错误”线从(x,y)=(-1.25,9.1)到(0.75,8.0)。 “右”线从(-1.25,9.7)到(0.75,7.5) 这是静态比较的代码:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
boring_fig = plt.figure()
blue = plt.plot([-1.25,.75], [9.1,8], color = 'b', label = 'wrong')
red = plt.plot([-1.25,.75], [9.7,7.5], color = 'r', label = 'right')
plt.show()

现在我想在蓝线所在的位置启动它们,然后将红线逐渐移动到正确的位置。我将这两个阵列用于y坐标,以在线之间逐步更改。然后,我绘制除红线以外的所有内容,希望在此之后将其添加为动画。

y_left = np.array([9.1, 9.16, 9.22, 9.28, 9.34, 9.4, 9.46, 9.52, 9.58, 9.64, 9.7])
y_right = np.array([8.0, 7.95, 7.9, 7.85, 7.8, 7.75, 7.7, 7.65, 7.6, 7.55, 7.5])

fig = plt.figure()
blue = plt.plot([-1.25,.75], [9.1,8], color = 'b', label = 'wrong')
plt.show()

然后,我尝试将红线段沿那些增量y值转移而动画。我在此处的某个地方遇到了错误:

def animate_1(i):
    return plt.plot([-1.25,.75], [y_left[i],y_right[i]], color = 'r'),
   
anim = FuncAnimation(fig = fig, func = animate_1, interval = 100, frames = 10)
plt.show(anim)

“我收到此消息:“用户沃宁:动画在没有渲染的情况下被删除。这很可能是意想不到的。要防止删除,只要您需要动画,就可以将动画分配给存在的变量。 。” 我花了几个小时来弄清楚这一点,但是我太多了。请帮忙。

Note: only the last code chunk brings an error. The earlier chunks are to give context to the animation that I want. This is all in Jupyter for Windows.

I have a matplotlib pyplot with two line segments, one is "wrong" and the other is "right." I want to animate the graph to start with both lines where the blue "wrong" line is,and have the red one pivot and move to be in the right place. The "wrong" line goes from (x,y) = (-1.25,9.1) to (0.75,8.0). The "right" line goes from (-1.25,9.7) to (0.75,7.5)
Here is the code for the static comparison:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
boring_fig = plt.figure()
blue = plt.plot([-1.25,.75], [9.1,8], color = 'b', label = 'wrong')
red = plt.plot([-1.25,.75], [9.7,7.5], color = 'r', label = 'right')
plt.show()

Now I want to start them both where the blue line is, and then have the red line incrementally move to the correct position. I made these two arrays for y coordinates to incrementally change between lines. Then I graph everything but the red line, in hopes of adding it as an animation after this.

y_left = np.array([9.1, 9.16, 9.22, 9.28, 9.34, 9.4, 9.46, 9.52, 9.58, 9.64, 9.7])
y_right = np.array([8.0, 7.95, 7.9, 7.85, 7.8, 7.75, 7.7, 7.65, 7.6, 7.55, 7.5])

fig = plt.figure()
blue = plt.plot([-1.25,.75], [9.1,8], color = 'b', label = 'wrong')
plt.show()

And then I try to animate the red line segment shifting along those incremented y values. I get the error somewhere in here:

def animate_1(i):
    return plt.plot([-1.25,.75], [y_left[i],y_right[i]], color = 'r'),
   
anim = FuncAnimation(fig = fig, func = animate_1, interval = 100, frames = 10)
plt.show(anim)

And I get this message: "UserWarning: Animation was deleted without rendering anything. This is most likely unintended. To prevent deletion, assign the Animation to a variable that exists for as long as you need the Animation."
I spent hours trying to figure this out, but I am too much of a noob. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱给你人给你 2025-02-15 06:41:33

事实证明,我必须为Windows安装FFMPEG:

https:// https://www.wikihow.com /install-ffmpeg-on-windows

现在该图显示了,但我看不到它在动画。那好吧。这是一个完全不同的问题。

这是我制作的结束图(带有几个额外的详细信息)

It turns out I had to install ffmpeg for windows:

https://www.wikihow.com/Install-FFmpeg-on-Windows

Now the graph shows up but I can't see it animating. Oh well. That is an entirely different question.

here is the end graph I made (with a couple of extra details)

浮华 2025-02-15 06:41:33

将此行添加到代码的开头,以查看动画作品:

%matplotlib notebook

在Jupyter Notebook(至少在Windows上),指定GUI工具包将允许您查看动画更新,而不仅仅是静态图形。您可以阅读有关工具包和用户接口的更多信息,在这里

如果有消息说:

Warning: Cannot change to a different GUI toolkit: ...

然后重新启动jupyter内核,然后再次运行单元格以更新GUI。有关GUI的故障排除,请参见 a>。

关于原始问题,如果您在funcanimation中设置blit = false,则应显示动画。否则,您可以将返回蓝色,语句添加到animate函数。请参阅下面的代码,随时提出问题!

完成代码

%matplotlib notebook

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation

def move_line(num_frames):
    x_vals = [-1.25,.75]
    y_left_array = np.linspace(9.1, 9.7, num=num_frames, endpoint=True)
    y_right_array = np.linspace(8.0, 7.5, num=num_frames, endpoint=True)
    
    fig = plt.figure()
    red = plt.plot(x_vals, [9.7,7.5], color = 'r', label = 'right')
    blue, = plt.plot([], [], color = 'b', label = 'wrong')

    def animate(I):
        y_vals = [y_left_array[i],y_right_array[I]]
        blue.set_data(x_vals,y_vals)
        return blue,

    anim = FuncAnimation(
        fig,
        animate,
        frames = num_frames,
        interval = 1000/30,
        repeat = False,
        blit = True
    )

    plt.show()

    return anim

animate_lines = move_line(100)

Add this line to the beginning of your code in order to see the animation work:

%matplotlib notebook

In Jupyter Notebook (at least on Windows) specifying the GUI toolkit will allow you to see the animation updating, not just a static graph. You can read more about toolkits and user interfaces here.

If there is a message that says:

Warning: Cannot change to a different GUI toolkit: ...

then restart the Jupyter kernel and run the cell again to update the GUI. For troubleshooting the GUI, see this answer.

In regards to the original question, if you set blit = False in FuncAnimation then the animation should display. Otherwise, you can add a return blue, statement to the animate function. See my code below and feel free to ask questions!

Complete Code

%matplotlib notebook

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation

def move_line(num_frames):
    x_vals = [-1.25,.75]
    y_left_array = np.linspace(9.1, 9.7, num=num_frames, endpoint=True)
    y_right_array = np.linspace(8.0, 7.5, num=num_frames, endpoint=True)
    
    fig = plt.figure()
    red = plt.plot(x_vals, [9.7,7.5], color = 'r', label = 'right')
    blue, = plt.plot([], [], color = 'b', label = 'wrong')

    def animate(I):
        y_vals = [y_left_array[i],y_right_array[I]]
        blue.set_data(x_vals,y_vals)
        return blue,

    anim = FuncAnimation(
        fig,
        animate,
        frames = num_frames,
        interval = 1000/30,
        repeat = False,
        blit = True
    )

    plt.show()

    return anim

animate_lines = move_line(100)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文