[FuncAnimation] 1. Random ๋ฐ์ดํฐ๋ก ์ค์๊ฐ ๊ทธ๋ํ ๋ง๋ค๊ธฐ
221021 ์์ฑ
<๋ณธ ๋ธ๋ก๊ทธ๋ redorangeyellowy๋์ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค>
https://operstu1.tistory.com/97
<matplotlib> ์ค์๊ฐ ๋ฐ์ดํฐ ๋ฐ์ ๊ทธ๋ํ
<๋ชฉ์ฐจ> 1. ๊ธฐ๋ณธ ๊ตฌ์กฐ 2. csv ํ์ผ ์ฝ์ด๋ค์ฌ ๋ง๋ค๊ธฐ 1. ๊ธฐ๋ณธ ๊ตฌ์กฐ ์ฌ์ฉํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ random -> ๋ฌด์์์ ์๋ฅผ ์์ฑํ๊ธฐ ์ํด ์ฌ์ฉํฉ๋๋ค. intertools.count -> 1,2,3... ์์ฐจ์ ์ธ ์๋ฅผ ์์ฑํ๊ธฐ ์ํด ์ฌ์ฉํฉ๋
operstu1.tistory.com
๐ ๋ฌด์์ ์๋ฅผ ์์ฑํด์ ์ค์๊ฐ ๊ทธ๋ํ ๋ง๋ค๊ธฐ 1
anim = FuncAnimation(fig, animate, frames=200, interval=50)
- Figure ๊ฐ์ฒด
- ํ๋ ์๋ง๋ค ๋ฐ๋ณตํด์ ํธ์ถํ ํจ์
- ๋ฐ๋ณต ๊ฐ๋ฅํ ๊ฐ์ฒด๋ฅผ ์ ๋ ฅ
- ํ๋ ์ ๊ฐ๊ฒฉ
# random ๋ฐ์ดํฐ
import random # ๋ฌด์์์ ์๋ฅผ ์์ฑ
from itertools import count # 1,2,3... ์์ฐจ์ ์ธ ์๋ฅผ ์์ฑ
import pandas as pd
# animation ํจ๊ณผ, ์ค์๊ฐ ๋ฐ์ดํฐ ๋ฐ์
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
plt.style.use('fivethirtyeight')
x_val = []
y_val = []
index = count()
def animate(i):
x_val.append(next(index)) # ์์ฐจ์ ์ธ ์๋ฅผ ์์ฑ
y_val.append(random.randint(0,5)) # 0~5 ์ฌ์ด์ ๋๋คํ ์ ์๋ฅผ ์์ฑ
plt.cla() # ์์ ๊ทธ๋ํ๋ฅผ ์ญ์
plt.plot(x_val, y_val)
ani = FuncAnimation(plt.gcf(), animate, interval = 1000)
# plt.gcf() : ํ์ฌ ๊ทธ๋ํ ๋ชจ์์ ๊ฐ์ ธ์ค๊ธฐ
# animate : ์ ๋๋ฉ์ด์
ํจ๊ณผ๋ฅผ ์ ์ฉ
# interval : 1000 -> 1000 ๋ฐ๋ฆฌ์ด ๋ง๋ค ์ ์ฉ
plt.tight_layout() # ์๋์ผ๋ก ๋ช
์๋ ์ฌ๋ฐฑ์ ๊ด๋ จ๋ ์๋ธํ๋กฏ ํ๋ผ๋ฏธํฐ ์กฐ์
plt.show()
๐ ๋ฌด์์ ์๋ฅผ ์์ฑํด์ ์ค์๊ฐ ๊ทธ๋ํ ๋ง๋ค๊ธฐ 2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig = plt.figure()
ax = plt.axes(xlim=(0, 4), ylim=(-5, 5))
line, = ax.plot([], [], lw=3)
def animate(i):
x = np.linspace(0, 4, 1000)
y = np.sin(2 * np.pi * (x - 0.05 * i))
line.set_data(x, y)
return line,
# anim = FuncAnimation(fig, animate, frames=200, interval=50)
anim = FuncAnimation(fig, animate, frames=100, interval=50)
plt.show()
๐ csvํ์ผ ๋ก๋ํด์ ์ค์๊ฐ ๊ทธ๋ํ ๋ง๋ค๊ธฐ
import random
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from pandas.core.indexes import interval
def load_file(data) :
df = pd.concat([data['insert_date_time'][:20000], data['cnt_mt1'][:20000]], axis = 1)
print(df)
return df
fig = plt.figure()
ax = plt.axes(xlim=(0, 10), ylim=(-10, 500))
line, = ax.plot([], [], lw=3)
def animate(i):
temp = pd.read_csv("/home/ubuntu/FPDS/20220929/sendinginfo_20220929.csv")
data = load_file(temp)
print(data.columns)
# data =pd.read_csv('data4.csv')
x = data['insert_date_time']
y1 = data['cnt_mt1']
# y2 = data['total_2']
plt.cla()
plt.plot(x,y1, label='insert_date_time')
line.set_data(x, y1)
return line,
# ani = FuncAnimation(plt.gcf(), animate, interval = 10)
ani = FuncAnimation(fig, animate, interval = 10)
# anim = FuncAnimation(fig, animate, frames=200, interval=50)
# (Figure ๊ฐ์ฒด, ํ๋ ์๋ง๋ค ๋ฐ๋ณตํด์ ํธ์ถํ ํจ์, ๋ฐ๋ณต ๊ฐ๋ฅํ ๊ฐ์ฒด๋ฅผ ์
๋ ฅ, ํ๋ ์ ๊ฐ๊ฒฉ)
plt.tight_layout()
plt.show()
# gif ์ ์ฅํ๊ธฐ
#graph_ani.save('graph_ani.gif', writer='imagemagick', fps=3, dpi=100)
โ ๋ค๋ฅธ ์ฝ๋๋ก ๋์
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pandas as pd
plt.style.use('seaborn')
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
def animation(i):
AAPL_STOCK = pd.read_csv('/home/ubuntu/FPDS/20220929/sendinginfo_20220929.csv')
AAPL_STOCK = AAPL_STOCK[:20000]
x = []
y = []
x = AAPL_STOCK[0:i]['insert_date_time']
y = AAPL_STOCK[0:i]['cnt_mt1']
ax.clear()
ax.plot(x, y)
animation = FuncAnimation(fig, func=animation, interval=1)
plt.show()
animation.save('animation1.gif', writer='imagemagick', fps=3, dpi=100)
โ ๋๊ฐ ๋ณ์ ๊ทธ๋ํ ๋์
import random
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from pandas.core.indexes import interval
fig = plt.figure()
ax = plt.axes(xlim=(0, 10), ylim=(-10, 500))
line, = ax.plot([], [], lw=3)
def animate(i):
data = pd.read_csv("/home/ubuntu/FPDS/20220929/sendinginfo_20220929.csv")
#data = load_file(temp)
data = data[:30000]
print(data.columns)
# data =pd.read_csv('data4.csv')
x = data['insert_date_time']
y1 = data['cnt_mt1']
y2 = data['cnt_wait']
plt.cla()
plt.plot(x,y1, label='cnt_mt1')
plt.plot(x,y2,label='cnt_wait')
plt.legend(loc = 'upper left')
plt.tight_layout()
line.set_data(x, y1)
return line,
# ani = FuncAnimation(plt.gcf(), animate, interval = 10)
ani = FuncAnimation(plt.gcf(), animate, interval = 100)
# (Figure ๊ฐ์ฒด, ํ๋ ์๋ง๋ค ๋ฐ๋ณตํด์ ํธ์ถํ ํจ์, ๋ฐ๋ณต ๊ฐ๋ฅํ ๊ฐ์ฒด๋ฅผ ์
๋ ฅ, ํ๋ ์ ๊ฐ๊ฒฉ)
plt.tight_layout()
plt.show()