簡體   English   中英

FuncAnimation 什么都不顯示

[英]FuncAnimation show nothing

我有在另一台機器上運行的代碼。

%matplotlib widget

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

n = 100
x = np.random.randn(n)

def update(curr):
    if curr == n:
        a.event_source.stop()
    plt.cla()
    bins = np.arange(-4, 4, 0.5)
    plt.hist(x[:curr], bins=bins)
    plt.axis([-4,4,0,30])
    plt.gca().set_title('Sampling the Normal Distribution')
    plt.gca().set_ylabel('Frequency')
    plt.gca().set_xlabel('Value')
    plt.annotate('n = {}'.format(curr), [3,27])
a = animation.FuncAnimation(plt.figure(), update, interval=100)

plt.show()

但是,它每次都給我這個

有時:“UserWarning: Animation was deleted without rendering anything. 這很可能不是故意的。為防止刪除,請將 Animation 分配給一個變量,例如anim ,該變量在您使用plt.show()anim.save()輸出動畫之前一直存在. anim.save() 。”

我安裝了 ipympl,重新啟動內核、IDE、計算機,刪除了“%matplotlib 小部件”,但這一切都沒有幫助。 我希望你能幫我一把

更新:我檢查了幾個工作代碼示例並發現了兩件事:

  1. 包括這段代碼在內的所有代碼都會生成正確的動畫,如果您將其保存為任何格式(mp4、html),您就可以看到它
  2. 如果動畫保存了,那么plt.show()會顯示最后一幀,如果沒有,則如上圖

我遇到了同樣的問題,我發現的唯一解決方法是保存動畫,然后從文件中讀回:

from IPython.display import Image
f = r'test_animation.gif'
writergif = animation.PillowWriter(fps=10)
a.save(f,writer=writergif)
plt.close()
Image(open('test_animation.gif','rb').read())

暫無
暫無

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

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