하나의 애니메이션에서 여러 데이터 세트를 동시에 플롯하기 위해 matplotlib로 애니메이션을 만들려고합니다. 문제는 내 데이터 집합 중 두 개가 50 점이고 세 번째 점은 70000 점입니다. 따라서 첫 번째 두 데이터 집합은 세 번째 데이터 집합이 표시되기 시작한 시점에 플롯팅되기 때문에 동시 점 (점 사이의 간격이 같은)은 쓸모가 없습니다.파이썬 matplotlib 여러 줄 애니메이션
따라서 데이터 세트를 별도의 애니메이션 호출 (즉, 다른 간격, 즉 플로팅 속도)으로 플롯해야하지만 단일 플롯에서 플롯해야합니다. 문제는 호출 된 마지막 데이터 세트에 대해서만 애니메이션이 표시된다는 것입니다.
아래 임의의 데이터에 대한 코드를 참조하십시오
import numpy as np
from matplotlib.pyplot import *
import matplotlib.animation as animation
import random
dataA = np.random.uniform(0.0001, 0.20, 70000)
dataB = np.random.uniform(0.90, 1.0, 50)
dataC = np.random.uniform(0.10, 0.30, 50)
fig, ax1 = subplots(figsize=(7,5))
# setting the axes
x1 = np.arange(0, len(dataA), 1)
ax1.set_ylabel('Percentage %')
for tl in ax1.get_xticklabels():
tl.set_color('b')
ax2 = ax1.twiny()
x2 = np.arange(0, len(dataC), 1)
for tl in ax2.get_xticklabels():
tl.set_color('r')
ax3 = ax1.twiny()
x3 = np.arange(0, len(dataB), 1)
for tl in ax3.get_xticklabels():
tl.set_color('g')
# set plots
line1, =ax1.plot(x1,dataA, 'b-', label="dataA")
line2, =ax2.plot(x2,dataC, 'r-',label="dataB")
line3, =ax3.plot(x3, dataB, 'g-', label="dataC")
# set legends
ax1.legend([line1, line2, line3], [line1.get_label(),line2.get_label(), line3.get_label()])
def update(num, x, y, line):
line.set_data(x[:num], y[:num])
line.axes.axis([0, len(y), 0, 1]) #[xmin, xmax, ymin, ymax]
return line,
ani = animation.FuncAnimation(fig, update, len(x1), fargs=[x3, dataB, line3],interval=150, blit=True, repeat=False)
ani = animation.FuncAnimation(fig, update, len(x1), fargs=[x1, dataA, line1],interval=5, blit=True, repeat=False)
ani = animation.FuncAnimation(fig, update, len(x1), fargs=[x2, dataC, line2],interval=150, blit=True, repeat=False)
# if the first two 'ani' are commented out, it live plots the last one, while the other two are plotted static
show()
다음과 같이한다 결국 줄거리 : http://i.imgur.com/RjgVYxr.png
하지만 포인트가 동시에 애니메이션을 얻을 수 있습니다 (그러나 서로 다른 보) 선을 그립니다.
고맙습니다! 그것은 작동합니다! – MVab