2014-09-15 5 views
3

pyplot의 funcanimation을 사용하여 동일한 그래프에서 다른 오브젝트에 애니메이션을 적용하려고합니다.
다른 요소가 표시되는 순서를 제외하고는 거의 예상대로 작동합니다. 그래서 플롯 곡선, 텍스트 및 범례가 거의 보이지 않는 이미지 뒤에 표시됩니다. 여기Pyplot zorder가 애니메이션과 함께 손실되었습니다.

내 (그렇지 않은) 최소한의 작업 예입니다

#! /usr/bin/python 
import numpy as np 
from matplotlib import pyplot as plt 
from matplotlib import animation 
import random 

def init(): 
    line_simu.set_data([], []) 
    time_text.set_text('') 
    imobj.set_data(np.zeros((100, 100))) 
    imobj.set_zorder(0) 
    time_text.set_zorder(10) 
    return line_simu, time_text, imobj 

def animate(i): 
    imobj.set_zorder(0) 
    time_text.set_zorder(10) 
    y_simu = np.linspace(0,100, 100) 
    x_simu = np.linspace(-10, 10, 100) 
    line_simu.set_data(x_simu, y_simu) 

    time_text.set_text('time = %.1f' % i) 

    global data 
    imobj.set_data(data + np.random.random((100,1)) * 0.5) 

    return line_simu, time_text, imobj 


def forceAspect(ax,aspect=1): 
    im = ax.get_images() 
    extent = im[0].get_extent() 
    ax.set_aspect(abs((extent[1]-extent[0])/(extent[3]-extent[2]))/aspect) 


fig = plt.figure() 
ax = plt.axes(xlim=(-15,15), ylim=(-110, 0) , aspect=1) 

data = np.random.random((100,100)) - .5 
imobj = ax.imshow(data , extent=[-15,15, -110, 0.0], origin='lower', cmap=plt.cm.gray, vmin=-2, vmax=2, alpha=.7, zorder=0, aspect=1) 

line_simu, = ax.plot([], [],"r--", lw=2, markersize=4 , label = "Some curve" , zorder= 2) 

time_text = ax.text(-14.9, -108, '', zorder=10) 

l = plt.legend(loc='lower right', prop={'size':8}) 
l.set_zorder(200) 

forceAspect(ax,aspect=1) 

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=range(50), interval=3000, blit=True) 

plt.show() 

애니메이션없이, 내가 쉽게 set_zorder와 다른 요소의 순서를 제어 할 수 있지만, 애니메이션 이미지를 업데이트 할 때,이 순서가 손실됩니다 . init 함수에서 zorder를 설정하고 animate 함수에서 성공하지 않고 다시 설정하려고했습니다.

그 문제에 대한 도움에 대해 매우 감사드립니다.

+0

그'당신이이 zOrder를이 무엇인지에 상관없이 표시되지 않도록, 축 범위 내에서 존재하지 않는이 예에서 업데이트하는 line_sumu' :

여기 내 수정 된 코드입니다. 그러나 그것을 고치면 실제로 1.3.1을 사용하여 zorder 문제없이 제대로 작동합니다. matplotlib의 버전은 무엇입니까? – Ajean

+0

감사합니다. Ajean. 경계선 밖의 선으로 문제를 편집했지만 설명하기 만했습니다. 그것은 당신을 위해 일한 좋은 소식입니다. 저는 실제로 구버전을 가지고 있습니다 : 1.1.1. 나는 그것을 업데이트하려고 노력할 것이고 당신에게 알려줄 것입니다. –

+0

마지막으로, matplotlib 1.3.1을 사용하여 스크립트를 테스트 할 기회를 얻었습니다. 불행히도 아무 것도 해결하지 못합니다. 커브/범례/타임 스트링은 투명도를 통해서만 볼 수 있으며, 이미지는 맨 위에 있고 플롯의 가독성을 떨어 뜨립니다. –

답변

1

내 자신의 질문에 대답하려면 : init() 및 animate() 함수 반환 순서는 개체가 표시되는 순서를 제어하는 ​​것 같습니다. 또한 해당 함수는 애니메이션에 포함하기 위해 범례 객체를 반환해야합니다. 내가 알

#! /usr/bin/python 
import numpy as np 
from matplotlib import pyplot as plt 
from matplotlib import animation 
import random 

def init(): 
    imobj.set_data(np.zeros((100, 100))) 
    line_simu.set_data([], []) 
    time_text.set_text('time = 0.0') 

    return imobj , line_simu, time_text, l 

def animate(i): 
    global data 
    imobj.set_data(data + np.random.random((100,1)) * 0.5) 

    imobj.set_zorder(0) 
    y_simu = np.linspace(-100,-10, 100) 
    x_simu = np.linspace(-10, 10, 100) 
    line_simu.set_data(x_simu, y_simu) 
    time_text.set_text('time = %.1f' % i) 


    return imobj , line_simu, time_text, l 


def forceAspect(ax,aspect=1): 
    im = ax.get_images() 
    extent = im[0].get_extent() 
    ax.set_aspect(abs((extent[1]-extent[0])/(extent[3]-extent[2]))/aspect) 



fig = plt.figure() 
ax = plt.axes(xlim=(-15,15), ylim=(-110, 0) , aspect=1) 

data = np.random.random((100,100)) - .5 
imobj = ax.imshow(data , extent=[-15,15, -110, 0.0], origin='lower', cmap=plt.cm.gray, vmin=-2, vmax=2, alpha=1.0, zorder=1, aspect=1) 

line_simu, = ax.plot([], [],"r--", lw=2, markersize=4 , label = "Some curve" , zorder= 1) 
time_text = ax.text(-14.0, -108, '', zorder=10) 

forceAspect(ax,aspect=1) 

l = plt.legend(loc='lower right', prop={'size':8}) 

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=range(50), interval=500, blit=True) 

plt.show() 
관련 문제