2016-06-17 6 views
0

하나의 애니메이션에서 여러 데이터 세트를 동시에 플롯하기 위해 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

하지만 포인트가 동시에 애니메이션을 얻을 수 있습니다 (그러나 서로 다른 보) 선을 그립니다.

답변

0

별도의 애니메이션 호출을 사용하는 것보다 간단한 것은 같은 애니메이션 호출에서 모든 행을 업데이트하지만 다른 속도로 업데이트하는 것입니다. 귀하의 경우에는 update으로 전화 할 때마다 (70000/50)마다 빨간색과 녹색 선만 업데이트하십시오.

는 다음에 update 기능에서 시작 코드를 변경하여이 작업을 수행 할 수 있습니다

def update(num): 
    ratio = 70000/50 
    i = num/ratio 
    line1.set_data(x1[:num], dataA[:num]) 
    if num % ratio == 0: 
     line2.set_data(x2[:i], dataC[:i]) 
     line3.set_data(x3[:i], dataB[:i]) 


ani = animation.FuncAnimation(fig, update, interval=5, repeat=False) 

show() 

참고 납입이 비율로 나누어 경우에만 다음 줄을 실행 if num % ratio == 0 문. 또한 더 느리게 업데이트되는 줄에 대해 별도의 카운터를 만들어야합니다. 이 경우 i을 사용했습니다.

+0

고맙습니다! 그것은 작동합니다! – MVab

관련 문제