๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[FuncAnimation] 1. Random ๋ฐ์ดํ„ฐ๋กœ ์‹ค์‹œ๊ฐ„ ๊ทธ๋ž˜ํ”„ ๋งŒ๋“ค๊ธฐ ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป ์ธ๊ณต์ง€๋Šฅ (ML & DL)/Serial Data

[FuncAnimation] 1. Random ๋ฐ์ดํ„ฐ๋กœ ์‹ค์‹œ๊ฐ„ ๊ทธ๋ž˜ํ”„ ๋งŒ๋“ค๊ธฐ

์ง•์ง•์•ŒํŒŒ์นด 2022. 10. 21. 13:32
728x90
๋ฐ˜์‘ํ˜•

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()

interval ๋‹ค๋ฅด๊ฒŒ ํ–ˆ์Œ

 

 

๐Ÿ’Ž 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()

728x90
๋ฐ˜์‘ํ˜•
Comments