簡體   English   中英

如何讓用 ImageMagick 制作的 animation 在我的 python Jupyter Notebook 上播放?

[英]How do I make animation made with ImageMagick play on my python Jupyter Notebook?

如何在我的 python Jupyter Notebook 上播放由 ImageMagick 制作的 animation?

我是Python上的動畫新手,所以我正在研究如何制作一個。 到目前為止,我從 DevelopPaper 復制了代碼,但是當我運行單元格時,它只顯示它的 static 圖像。 我也在那里嘗試了其他 animation 示例,但它們都是一樣的——來自 Imagemagick 的動畫不會在 Jupyter Notebook 上播放,但是當我手動打開保存的文件時,它工作得很好。 如何讓動畫在 Jupyter Notebook 上播放? 謝謝! 在此處輸入圖像描述

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
plt.style.use('seaborn-pastel')


fig = plt.figure()
ax = plt.axes(xlim=(0, 4), ylim=(-2, 2))
line, = ax.plot([], [], lw=3)

def init():
    line.set_data([], [])
    return line,
def animate(i):
    x = np.linspace(0, 4, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

anim = FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)

anim.save('sine_wave.gif', writer='imagemagick')

有很多選擇。 (而且您可能不容易找到解決方案,因為您沒有使用帖子標題所反映的正確術語。您似乎想在 Jupyter 中播放.gif或由幀組成的動畫 plot。)

首先,解決在 Jupyter 中顯示 gif 的問題:

最簡單的是使用 JupyterLab 打開一個新的筆記本,然后點擊這里運行你的代碼。 要在那里使用您的代碼,請將最后一行更改為anim.save('sine_wave.gif')因為我還沒有費心在那里安裝 imagemagick。Pillow 能夠制作 gif,而 matplotlib 將處理選擇 Pillow task 如果你只是不指定writer 。)

生成gif文件后,您只需在左側的文件瀏覽器中雙擊它即可播放。 您可以通過單擊其上方的選項卡並拖動然后根據需要釋放它來根據需要相對於筆記本安排它播放的 window。

如果您更喜歡在更傳統的筆記本中播放 gif,則可以在生成 gif 文件后將以下代碼簡單地放在一個單元格中:

from IPython.display import Image
Image("sine_wave.gif")

如果 gif 圖像文件與筆記本文件位於同一文件夾中,則執行該筆記本單元將為該單元播放 gif 作為 output。

其次,顯示備選方案而不是制作 gif:

對於如何正確地播放繪圖 animation,不一定是 gif,我建議在示例和鏈接之后添加控件到筆記本底部,您可以通過單擊此處的“啟動活頁夾”以活動形式打開 您正在尋找在底部使用或涉及FuncAnimation()的示例和鏈接。 我建議涉及框架的那些允許您使用小部件控制和播放它們,或者制作可以播放和控制的 HTML。 重要的是,即使筆記本處於“靜態”狀態,該小部件方法也能正常工作,如您在此處所見。 我將static放在引號中,因為一些活動功能將在 nbviewer 中工作,但通常會在 Github 預覽中注明,現在 Github 嘗試對筆記本進行預覽渲染,即使是相當長的筆記本,這也會讓很多新手感到困惑。 (如果您需要生成可移植的 HTML5 兼容視頻文件,請參閱同一部分。)這是您的代碼的幾個變體,使用相關方法添加將在此處的活動會話中工作的小部件/框架生成:

一個選項

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML, display
plt.style.use('seaborn-pastel')

def generate_animation():
    fig = plt.figure()
    ax = plt.axes(xlim=(0, 4), ylim=(-2, 2))
    lineplot, = ax.plot([], [], lw=3)
    
    def init():
        lineplot.set_data([], [])
        return lineplot, #return [lineplot] also works like in https://nbviewer.org/github/raphaelquast/jupyter_notebook_intro/blob/master/jupyter_nb_introduction.ipynb#pre-render-animations-and-export-to-HTML

    def animate(i):
        x = np.linspace(0, 4, 1000)
        y = np.sin(2 * np.pi * (x - 0.01 * i))
        lineplot.set_data([x], [y])
        return [lineplot]

    anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)

    display(HTML(anim.to_jshtml()))
    plt.close(fig)

generate_animation()

另一種變體

# RUN THIS TWICE. SECOND TIME WILL BE NICE SINGLE PLOT DISPLAYED.
# January 2023 I was finding first time I ran this or code like at https://nbviewer.org/gist/fomightez/d862333d8eefb94a74a79022840680b1 that it output a non-interactive frame of plot, too. Just re-ran and then it is just the interactive one with widget.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML, display
plt.rcParams["animation.html"] = "jshtml"
plt.ioff() #needed so the second time you run it you get only single plot
plt.style.use('seaborn-pastel')

fig = plt.figure()
ax = plt.axes(xlim=(0, 4), ylim=(-2, 2))
lineplot, = ax.plot([], [], lw=3)
    
def init():
    lineplot.set_data([], [])
    return lineplot, #return [lineplot] also works like in https://nbviewer.org/github/raphaelquast/jupyter_notebook_intro/blob/master/jupyter_nb_introduction.ipynb#pre-render-animations-and-export-to-HTML

def animate(i):
    x = np.linspace(0, 4, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    lineplot.set_data([x], [y])
    return [lineplot]

anim = animation.FuncAnimation(fig, animate, init_func=init,
                           frames=200, interval=20, blit=True)
anim

一個 Jupyter notebook,其中包含運行選項和更全面的解釋 您會注意到小部件在那里工作。
單擊此處在通過 MyBinder.org 提供的活動的臨時 Jupyter session 中啟動該筆記本,環境中已經有 Python 以及使帶有小部件的 animation 在筆記本中安裝和運行所需的一切。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM