アニメーションを生成するプログラムコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import matplotlib.animation as animation | |
np.random.seed(0) | |
#データの生成 | |
def make_sin(x, amp, T=100): | |
noise = amp * np.random.randint(-10, 10, len(x)) | |
return np.sin(2*np.pi*x/T) + noise | |
x = np.arange(200) | |
y = make_sin(x, 0.01) | |
l = 25 | |
def make_dataset(y, l): | |
data = [] | |
target = [] | |
for i in range(len(y)-l): | |
data.append(y[i:i+l]) | |
target.append(y[i + l]) | |
return(data, target) | |
(data, target) = make_dataset(y, l) | |
data = np.array(data).reshape(175, 25, 1) | |
#プロットする | |
fig = plt.figure() | |
#グラフの表示範囲を指定 | |
plt.xlim(-10, 210) | |
plt.ylim(-1.5, 1.5) | |
#グラフ全体を薄く表示 | |
plt.plot(x, y, color='lightgray') | |
#アニメーション生成の前準備 | |
ims = [] | |
for i in range(175): | |
im1 = plt.plot(x[i: i+25], y[i: i+25], color='blue') | |
im2 = plt.plot(x[i+25], y[i+25], color='red', marker = 'o', markersize = 10) | |
ims.append(im1) | |
ims.append(im2) | |
#アニメーションの生成 | |
ani = animation.ArtistAnimation(fig, ims,interval=200) | |
#GIFファイルとして保存 | |
ani.save('output.gif', writer='imagemagick') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment