matplotlib를 사용하여 산점도 애니메이션을 저장하려면 다음 단계를 수행할 수 있습니다. -
- 그림 크기를 설정하고 서브플롯 사이 및 주변 여백을 조정합니다.
- 4가지 변수, 단계, 노드, 위치 및 솔루션을 초기화합니다.
- 목록에 위치 및 솔루션 값을 추가합니다.
- 그림과 서브플롯 세트를 생성합니다.
- 마커 크기에 대한 변수를 초기화합니다.
- 격자선을 구성합니다.
- *animate 함수를 반복적으로 호출하여 애니메이션 만들기 *, 축을 지우려면 새 축 서브롯을 추가하고 축에 산점도를 그립니다.
- 애니메이션 산점도를 .gif로 저장 파일.
예시
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
steps = 50
nodes = 100
positions = []
solutions = []
for i in range(steps):
positions.append(np.random.rand(2, nodes))
solutions.append(np.random.random(nodes))
fig, ax = plt.subplots()
marker_size = 50
def animate(i):
fig.clear()
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False, xlim=(0, 1), ylim=(0, 1))
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
s = ax.scatter(positions[i][0], positions[i][1], s=marker_size, c=solutions[i], cmap="RdBu_r", marker="o", edgecolor='black')
plt.grid(b=None)
ani = animation.FuncAnimation(fig, animate, interval=100, frames=range(steps))
ani.save('animation.gif', writer='pillow') 출력
