Animation 예제입니다.

(참고링크)

import numpy as np
import matplotlib.pyplot as plt
import math
import matplotlib.animation as animation

def B(alpha, beta):
    return math.gamma(alpha) * math.gamma(beta) / math.gamma(alpha + beta)

def beta_pdf(x, alpha, beta):
# [0, 1] 구간 밖에서는 밀도가 없음
    if x < 0 or x > 1:
        return 0
    return x ** (alpha - 1) * (1 - x) ** (beta - 1) / B(alpha, beta)

x_data = []
y_data = []

fig, ax = plt.subplots()
ax.set_xlim(0, 1)
ax.set_ylim(0, 12)
line, = ax.plot(0, 0)

def animation_frame(i):
    xs = [x / 100.0 for x in range(0,100)]

    x_data = [x / 100.0 for x in range(0,100)]
    y_data = [beta_pdf(x, alpha=i,beta=i+10) for x in xs]

    line.set_xdata(x_data)
    line.set_ydata(y_data)

    return

anim = animation.FuncAnimation(fig, func=animation_frame, frames=np.arange(0.1, 80, 1), interval=50, repeat=False)
plt.show()

Jupyter notebook에서 예제를 시험할 때, animation이 동작하지 않을 수 있습니다.

이 경우에는 아래 코드를 먼저 실행한 후, 위 코드를 수행하면 animation 동작을 확인할 수 있습니다.

(참고링크)

%matplotlib notebook

 

 

반응형

'프로그래밍 > Python' 카테고리의 다른 글

[Python] Unicode error  (0) 2021.11.23
[Python] 디버깅  (0) 2020.05.03
[Python] 폴더 생성, tar 예제  (0) 2020.05.03
[Python] matplotlib 예제  (0) 2020.05.03

+ Recent posts