2014-01-12 5 views
3

프리젠 테이션 데이터에 애니메이션을 적용하려고합니다. 파이썬 애니메이션 패키지를 사용하려고합니다. 내가 뭘하려고 나는 내가 무슨 일이 일어나고 있는지 이해 사투를 벌인거야 파이썬 새로운 오전 http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/다른 색상의 선을 애니메이트하십시오.

import numpy as np 
from matplotlib import pyplot as plt 
from matplotlib import animation 

# First set up the figure, the axis, and the plot element we want to animate 
fig = plt.figure() 
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2)) 
line, = ax.plot([], [], lw=2) 

# initialization function: plot the background of each frame 
def init(): 
    line.set_data([], []) 
    return line, 

# animation function. This is called sequentially 
def animate(i): 
    x = np.linspace(0, 2, 1000) 
    y = np.sin(2 * np.pi * (x - 0.01 * i)) 
    line.set_data(x, y) 
    return line, 

# call the animator. blit=True means only re-draw the parts that have changed. 
anim = animation.FuncAnimation(fig, animate, init_func=init, 
          frames=200, interval=20, blit=True) 

의 첫 번째 예에 대략 요약된다. 나에게 그것은 init()이나 animate (i)가 반환하는 것을 수정하지 않는 것으로 보인다. 또한 수정중인 객체 (선이 아닌)는 이전에 선언되지 않았습니다.

어느 쪽이든, 내가하려는 것은 데이터,이 경우 사인파, 조각 별 색깔을 갖는 것입니다. 0과 1 사이의 파란색, 1과 1.5 사이의 빨간색, 1.5와 2 사이의 파란색. 나는 많은 일을 시도했지만 이것이 작동하지 않습니다. 필자는 그 함수들이 전체 그림을 반환하려고 시도 했었습니다. 라인뿐만 아니라, 미리보기 그림을 플러시하고 내가 그린 합성 선을 음영 처리하기를 바랬습니다.

이 프레임 워크에서 어떻게 다른 색상의 선으로 구성된 선을 애니메이션으로 만들 수 있습니까?

답변

2

속성이 변경되는 선을 그리려면 LineCollection을 사용하십시오. 예 herehere. LineCollection을 사용하여 애니메이션을 만들려면 this 예제를 참조하십시오.

animate() 내에서 코드가 작동하는 방식에 대한 답변을 얻으려면 을 사용하여 x,y 속성을 재설정해야합니다. 함수는 matplotlib에 각 프레임에서 matplotlib가 업데이트해야하는 아티스트의 집합 (line,)을 반환합니다.

여기에 당신이 (당신의 플랫폼이 마지막 호출을 지원하는 경우에만 blit=true 가능)을 찾고 생각입니다 :

import numpy as np 
from matplotlib import pyplot as plt 
from matplotlib import animation 
from matplotlib.collections import LineCollection 
from matplotlib.colors import ListedColormap, BoundaryNorm 

# First set up the figure, the axis, and the plot element we want to animate 
fig = plt.figure() 
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2)) 

cmap = ListedColormap(['b', 'r', 'b']) 
norm = BoundaryNorm([0, 1, 1.5, 2], cmap.N) 

line = LineCollection([], cmap=cmap, norm=norm,lw=2) 
line.set_array(np.linspace(0, 2, 1000)) 
ax.add_collection(line) 

# initialization function: plot the background of each frame 
def init(): 
    line.set_segments([]) 
    return line, 

# animation function. This is called sequentially 
def animate(i): 
    x = np.linspace(0, 2, 1000) 
    y = np.sin(2 * np.pi * (x - 0.01 * i)) 
    points = np.array([x, y]).T.reshape(-1, 1, 2) 
    segments = np.concatenate([points[:-1], points[1:]], axis=1) 
    line.set_segments(segments) 
    return line, 

# call the animator. blit=True means only re-draw the parts that have changed. 
anim = animation.FuncAnimation(fig, animate, init_func=init, 
          frames=200, interval=20) 

enter image description here

+0

덕분에 많이. 나는 LineCollection에 대해 몰랐다. – Mathusalem

+0

그래서 선은 음모에 속하는 것으로 이해합니다. 음모는 이제 세그먼트의 모음입니다. 그러나 animate의 정의에서이를 어떻게 수정할 수 있습니까? 모든 반복마다 BoundaryNorm과 ListedColormap을 업데이트 할 수 있기를 원하지만, animate의 정의 내에서 line = ...을 선언 할 수 없습니다. 내부에있는 것뿐만 아니라 매개 변수로 라인 자체를 업데이트하는 방법이 있습니까? 내가 그것을하기를 원한다면, 실제로 선 (line) 대신에 전체 그림을 업데이트해야만 하는가? – Mathusalem

+0

은'line.set_cmap'과'line.set_norm'을 사용합니다. 그리고 전체 그림을 업데이트 할 필요는 없습니다. – gg349

관련 문제