Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python에서 등고선 플롯에 애니메이션을 적용하기 위해 matplotlib.animate를 사용하는 방법은 무엇입니까?

<시간/>

Python의 matplotlib에서 등고선 플롯에 애니메이션을 적용하려면 다음 단계를 수행할 수 있습니다.

  • 모양이 10☓10 차원인 임의의 데이터를 만듭니다.
  • subplots()를 사용하여 그림과 서브플롯 세트 생성 방법.
  • *func* 함수를 반복적으로 호출하여 애니메이션을 만듭니다. FuncAnimation() 사용 수업.
  • 함수의 윤곽 값을 업데이트하기 위해 FuncAnimation()에서 사용할 수 있는 애니메이션 메서드를 정의할 수 있습니다. 수업.
  • 그림을 표시하려면 show()를 사용하세요. 방법.

예시

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
data = np.random.randn(800).reshape(10, 10, 8)
fig, ax = plt.subplots()
def animate(i):
ax.clear()
ax.contourf(data[:, :, i])
ani = animation.FuncAnimation(fig, animate, 5, interval=50, blit=False)
plt.show()

출력

Python에서 등고선 플롯에 애니메이션을 적용하기 위해 matplotlib.animate를 사용하는 방법은 무엇입니까?