프로그램
[파이썬] Plot 애니메이션 (matplotlib.animation)
오디세이99
2023. 7. 18. 13:53
728x90
반응형
'''###########################
plot animation 기본
###########################'''
import pandas_datareader as pdr
from pandas import to_numeric
import datetime
from datetime import timedelta
import numpy as np
import pandas as pd
import warnings
from scipy.ndimage import gaussian_filter1d
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
code = '069500'
name = 'KODEX200'
# code = '006260'
# name = 'LS'
test_start = '2022-01-01'
test_end = '2023-12-31'
# 데이터 생성
last_day = 352
df = pdr.DataReader(code, "naver", test_start, test_end)
df = df.apply(to_numeric)
df.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
# plot 속성 지정
fig, ax = plt.subplots(figsize=(12,6))
ax.set_xlim(0, len(df))
ax.set_ylim(min(df['Close']), max(df['Close']))
ax.grid()
# plot에 그려질 list 변수 지정
x, y = [], []
avg = []
# plot에 그려질 plot 지정
pt_close, = plt.plot([], label='Close')
pt_avg, = plt.plot([], label='Avg')
def update(frame):
idx = len(x)
x.append(len(x))
y.append(frame)
avg.append((df['High'][idx] + df['Low'][idx] + df['Open'][idx]) / 3)
pt_close.set_data(x, y)
pt_avg.set_data(x, avg)
return frame,
%matplotlib qt5
# %matplotlib inline
ani = FuncAnimation(fig, update, frames=df['Close'].tolist())
# plt.show()
ani.save('stock_plot.gif', writer='imagemagick')
728x90
반응형