2017-09-15 8 views
5

일부 seaborn 메서드는 JointPlotcreate new figures on each call과 같습니다. 따라서 matplotlib과 같은 간단한 애니메이션을 만들 수 없으므로 plt.cla() 또는 plt.clf()으로 반복 호출하면 매번 창을 닫거나 열지 않고 그림의 내용을 업데이트 할 수 있습니다. 나는 현재 참조애니메이션의 시폰 그림 다시 그리기

유일한 솔루션입니다 : 우리가 바로 새로운 하나를 만들기 전에 이전 창을 닫습니다 때문에

for t in range(iterations): 
    # .. update your data .. 

    if 'jp' in locals(): 
     plt.close(jp.fig) 

    jp = sns.jointplot(x=data[0], y=data[1]) 
    plt.pause(0.01) 

이 작동합니다. 물론 이것은 이상과는 거리가 멀다.

더 좋은 방법이 있습니까? 플롯을 이전에 생성 된 Figure 개체에서 직접 수행 할 수 있습니까? 아니면 이러한 방법으로 각 통화에서 새로운 수치를 생성하지 못하도록하는 방법이 있습니까?

답변

4

불행히도 sns.jointplot은 그림 자체를 만듭니다. jointplot에 애니메이션을 적용하려면 각 interation에서 새로운 것을 재 작성하는 대신이 생성 된 figure를 재사용 할 수 있습니다.

jointplot은 내부적으로 JointGrid을 생성하므로이 값을 직접 사용하고 접합 축과 여백을 개별적으로 표시하는 것이 좋습니다. 그런 다음 애니메이션의 각 단계에서 데이터를 업데이트하고 축을 지우고 그리드 생성과 마찬가지로 축을 설정합니다. 불행히도,이 마지막 단계는 많은 코드 라인을 필요로합니다. 나는 이런 식으로 뭔가를 의심했다

import matplotlib.pyplot as plt 
import matplotlib.animation 
import seaborn as sns 
import numpy as np 

def get_data(i=0): 
    x,y = np.random.normal(loc=i,scale=3,size=(2, 260)) 
    return x,y 

x,y = get_data() 
g = sns.JointGrid(x=x, y=y, size=4) 
lim = (-10,10) 

def prep_axes(g, xlim, ylim): 
    g.ax_joint.clear() 
    g.ax_joint.set_xlim(xlim) 
    g.ax_joint.set_ylim(ylim) 
    g.ax_marg_x.clear() 
    g.ax_marg_x.set_xlim(xlim) 
    g.ax_marg_y.clear() 
    g.ax_marg_y.set_ylim(ylim) 
    plt.setp(g.ax_marg_x.get_xticklabels(), visible=False) 
    plt.setp(g.ax_marg_y.get_yticklabels(), visible=False) 
    plt.setp(g.ax_marg_x.yaxis.get_majorticklines(), visible=False) 
    plt.setp(g.ax_marg_x.yaxis.get_minorticklines(), visible=False) 
    plt.setp(g.ax_marg_y.xaxis.get_majorticklines(), visible=False) 
    plt.setp(g.ax_marg_y.xaxis.get_minorticklines(), visible=False) 
    plt.setp(g.ax_marg_x.get_yticklabels(), visible=False) 
    plt.setp(g.ax_marg_y.get_xticklabels(), visible=False) 


def animate(i): 
    g.x, g.y = get_data(i) 
    prep_axes(g, lim, lim) 
    g.plot_joint(sns.kdeplot, cmap="Purples_d") 
    g.plot_marginals(sns.kdeplot, color="m", shade=True) 

frames=np.sin(np.linspace(0,2*np.pi,17))*5 
ani = matplotlib.animation.FuncAnimation(g.fig, animate, frames=frames, repeat=True) 

plt.show() 

enter image description here

+0

: 같은

마지막 코드는 보일 수 있습니다. 나는 그것이 더 솔직하게 되길 바랄 것이지만 이것은 일이 멋지게 완성되도록합니다. 정말 고마워요. – runDOSrun

+0

seaborn의 소스 코드에서 음영을 애니메이션으로 표현할 수있는 가능성이 분명하게 드러나지 않았 음을 알 수 있습니다. 궁극적 인 목표에 따라 일부 최적화가 수행 될 수 있습니다. JointGrid를 서브 클래 싱하여 업데이트에 좀 더 취약하게 만들고 새로운 모듈에 넣고 필요할 때 호출하는 방법을 생각하고 있습니다. 그러나 그러한 애니메이션을 더 자주 수행해야하는 경우에만 의미가 있습니다. seaborn은 주로 matplotlib을 감싸므로 해답은 matplotlib만으로 jointplot이하는 것을 복제 할 수 있다는 것을 명심하십시오. – ImportanceOfBeingErnest