无法使用Funcanimation动画

发布于 2025-01-21 14:50:41 字数 887 浏览 1 评论 0 原文

我正在使用Geekforgeek中的代码,并将其复制到Jupyternotebook。

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

fig = plt.figure(figsize = (7,5))
axes = fig.add_subplot(1,1,1)
axes.set_ylim(0, 300)
palette = ['blue', 'red', 'green',
        'darkorange', 'maroon', 'black']

y1, y2, y3, y4, y5, y6 = [], [], [], [], [], []

def animation_function(i):
    y1 = i
    y2 = 5 * i
    y3 = 3 * i
    y4 = 2 * i
    y5 = 6 * i
    y6 = 3 * i

    plt.xlabel("Country")
    plt.ylabel("GDP of Country")
    
    plt.bar(["India", "China", "Germany",
            "USA", "Canada", "UK"],
            [y1, y2, y3, y4, y5, y6],
            color = palette)

plt.title("Bar Chart Animation")

animation = FuncAnimation(fig, animation_function,
                        interval = 50)
plt.show()

我只能看到图像而不是动画。如何解决? 。

I am using code from geekforgeek and copying it to jupyternotebook.

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

fig = plt.figure(figsize = (7,5))
axes = fig.add_subplot(1,1,1)
axes.set_ylim(0, 300)
palette = ['blue', 'red', 'green',
        'darkorange', 'maroon', 'black']

y1, y2, y3, y4, y5, y6 = [], [], [], [], [], []

def animation_function(i):
    y1 = i
    y2 = 5 * i
    y3 = 3 * i
    y4 = 2 * i
    y5 = 6 * i
    y6 = 3 * i

    plt.xlabel("Country")
    plt.ylabel("GDP of Country")
    
    plt.bar(["India", "China", "Germany",
            "USA", "Canada", "UK"],
            [y1, y2, y3, y4, y5, y6],
            color = palette)

plt.title("Bar Chart Animation")

animation = FuncAnimation(fig, animation_function,
                        interval = 50)
plt.show()

I am able to see only image not the animation. How to solve it?this is what I getthis is what I should should get

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

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

发布评论

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

评论(1

软糖 2025-01-28 14:50:41

您可能需要安装 ipympl module 。您可以使用 PIP安装ipympl conda install -c conda -forge ipympl 安装它。

然后,当您需要一个交互式框架来包装Matplotlib的输出(例如动画的情况)时,您可以在笔记本顶部执行以下命令:%matplotlib widget

然后,您使用动画执行单元格,并“神奇地”将在屏幕上渲染。

You probably need to install the ipympl module. You can install it with pip install ipympl or with conda install -c conda-forge ipympl.

Then, when you need an interactive frame to wrap Matplotlib's output (such as is the case for animations), you execute the following command at the top of your notebook: %matplotlib widget.

Then, you execute your cell with the animation, and "magically" it will be rendered on the screen.

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