2014-12-09 3 views
-1

매끄러운 선과 같은 그래프를 그릴 때 matplotlib를 사용하고 있습니다. 나를 그리는 것은 문제가 아니지만 애니메이션에 문제가 있습니다.부드럽고 매끄러운 그림 선 파이썬 matplotlib

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

그래서, 나는 다음과 같은 배열이 있습니다

a = [0,1,2,4,5,8,9,12,14,18,22,17,30,37,29,45] 

을 그리고 나는 그것을 부드럽게 지점을 그릴 필요가있다. ,

# First set up the figure, the axis, and the plot element we want to animate 
fig = plt.figure() 
ax = plt.axes(xlim=(0, 50), ylim=(0, 50)) 
line, = ax.plot([], [], lw=2) 
global x 
global y 
global n 
global a 
n=5 
a = [0,1,2,4,5,8,9,12,14,18,22,17,30,37,29,45] 

global desty,destx 
desty = np.linspace(0,0,n) 
destx = np.linspace(0,0,n) 
for i in range(len(a)-1): 
    desty = np.append(desty,np.linspace(a[i],a[i+1],n)) 
    destx = np.append(destx,np.linspace(i,i+1,n)) 
# initialization function: plot the background of each frame 
def init(): 
    line.set_data([], []) 
    return line, 
# animation function. This is called sequentially 
def animate(i): 
    global destx 
    global desty 
    x=destx 
    y=desty 
    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) 
plt.show() 

날 만 그릴 수 있습니다 : 나를

전체 코드 문제없이 선 (및 애니메이션을 :)) 그릴 수

for i in range(len(a)-1): 
    desty = np.append(desty,np.linspace(a[i],a[i+1],n)) 
    destx = np.append(destx,np.linspace(i,i+1,n)) 

: 지금 나는이 문자열을 가지고 하지만 어떻게하면 느리고 부드러운 지점을 그릴 수 있습니까? 내가 파이썬 2.7를 사용하고

, CentOS는 7

답변

2

당신은 슬림하여 음모를 꾸미고 루틴에 수 아래로 :

import numpy as np 
import random as random 
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, 50), ylim=(0, 50)) 
line, = ax.plot([], [], lw=2) 

n=5 
a = [0,1,2,4,5,8,9,12,14,18,22,17,30,37,29,45] 

x = [] 
y = [] 

# 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.append(np.linspace(i,i+1,n)) 
    y.append(np.linspace(a[i],a[i+1],n)) 
    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, np.arange(0,len(a)-1) ,init_func=init, 
           interval=200, blit=True, repeat=False) 

plt.show() 

주의하십시오 :

  • 당신은 global을 필요로하지 않습니다 변수가 작동하려면
  • xy을 애니메이션 외부에 설정하고 싶지 않습니다. (애니메이션에서는 전체 그래프 만 그릴 수 있도록 완전한 xy을 설정했습니다.)
  • animate (i)에 상호 작용을 전달해야합니다. 이것은 FuncAnimation - np.arange(0,len(a)-1)의 세 번째 입력입니다.
  • 하나의 실행 후 중지하고 '폐쇄'
  • 나는 음모가 느린
  • 당신은 애니메이션 기능에 i를 사용할 필요가
+0

'matplotlib.animation' API를 알려 주셔서 감사합니다. – heltonbiker

+0

감사합니다. 그게 효과가있어, 나 아주 도와 줘! – shatalov

0

, 그렇지 않으면 단지 플롯을 개발하기 위해 interval 증가 곡선을 피하기 위해 repeat=False 설정 똑같은 일이 반복해서 일어납니다. 줄거리는 각 애니메이션에서 각 반복에서 다른 모습, i해야 인덱스 무언가를 다르게하는

import numpy as np 
import random as random 
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, 50), ylim=(0, 50)) 
line, = ax.plot([], [], lw=2) 
global x 
global y 
global n 
global a 
n=5 
a = [0,1,2,4,5,8,9,12,14,18,22,17,30,37,29,45] 

global desty,destx 
desty = np.linspace(0,0,n) 
destx = np.linspace(0,0,n) 
plot_me_x = [] 
plot_me_y = [] 
for i in range(len(a)-1): 
    desty = np.append(desty, np.linspace(a[i],a[i+1],n)) 
    destx = np.append(destx, np.linspace(i,i+1,n)) 
    plot_me_x.append(np.array(destx)) # keep a copy of the intermediaries to plot later 
    plot_me_y.append(np.array(desty)) 
# initialization function: plot the background of each frame 
def init(): 
    line.set_data([], []) 
    return line, 
# animation function. This is called sequentially 
def animate(i): 
    global destx 
    global desty 
    x=plot_me_x[i] 
    y=plot_me_y[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=len(a)-1, interval=20, blit=True) 
plt.show() 

. 빌드를 반복하는 과정에서 점차적으로 destydestx이 추가되지만 최종 결과는 점진적 빌드가 아니라 단일 빌드이므로이 빌드의 중개자를 저장해야합니다. 나는 plot_me_xy으로 이것을했습니다.

오류의 위치를 ​​명확히하기 위해 OP 코드의 최소한의 변경으로이 답변을 작성했습니다. 전반적으로, @Schorsch가 만든 변경 사항은 전체적으로 더 깨끗한 접근법 (예 : 전역이없는)으로 이어졌습니다.

+0

감사합니다. 매우 유용한 답변입니다! – shatalov