|
実行結果の動画です。ループ再生します。
Python 3.11.6 (matplotlib 3.7.1) と Python 3.12.0 (matplotlib 3.8.1) 共に、下記のような警告があり、無限ループです。
無限ループを、回避するため提案の通り save_count=MAX_FRAMES を追加し、長さ 60 秒の動画を作成しました。
E:\____\dynamic_image.py:31: UserWarning:
frames=None which we can infer the length of, did not pass an explicit
*save_count* and passed cache_frame_data=True.
To avoid a possibly unbounded cache, frame data caching has been disabled.
To suppress this warning either pass `cache_frame_data=False` or
`save_count=MAX_FRAMES`.
ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
下記のように、save_count=900 を追加しました。
"""
=================
An animated image
=================
This example demonstrates how to animate an image.
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
def f(x, y):
return np.sin(x) + np.cos(y)
x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
im = plt.imshow(f(x, y), animated=True)
def updatefig(*args):
global x, y
x += np.pi / 15.
y += np.pi / 20.
im.set_array(f(x, y))
return im,
ani = animation.FuncAnimation(fig, updatefig, interval=50,
blit=True, save_count=900 )
ani.save('dynamic_image_2.mp4', fps=15)
plt.show()
実行結果の動画(dynamic_image_2.mp4)です。ループ再生します。
|