본문 바로가기
프로그램

[파이썬] 주식 Plot 애니메이션 (matplotlib.animation, FuncAnimation)

by 오디세이99 2023. 7. 18.
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 = '2020-01-01'
test_end = '2023-12-31'
GAMMA = 4

# 데이터 생성
last_day = 352
df = pdr.DataReader(code, "naver", test_start, test_end)
df = df.apply(to_numeric)
df.columns = ['Open', 'High', 'Low', 'Close', 'Volume']

fig, ax = plt.subplots(figsize=(12,6))
ax.set_xlim(0, len(df))
ax.set_ylim(min(df['Close']), max(df['Close']))
ax.grid()

x, y = [], []
pt_close, = plt.plot([], label='Close')
pt_ga, = plt.plot([], label='Gaussian')

def update(frame):
    x.append(len(x))
    y.append(frame)
    pt_close.set_data(x, y)
    g = gaussian_filter1d(y, sigma=4)
    pt_ga.set_data(x, g)
    return frame,


%matplotlib qt5
# %matplotlib inline
ani = FuncAnimation(fig, update, frames=df['Close'].tolist())
# plt.show()
ani.save('stock_gaussian.gif', writer='imagemagick')

728x90
반응형

댓글