Skip to content

Instantly share code, notes, and snippets.

@AGIRobots
Created December 24, 2019 12:40
Show Gist options
  • Save AGIRobots/de1e9abf978997074d87d24ea5cc99e0 to your computer and use it in GitHub Desktop.
Save AGIRobots/de1e9abf978997074d87d24ea5cc99e0 to your computer and use it in GitHub Desktop.
アニメーションを生成するプログラムコード
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