2017-04-12 1 views
1

두 개의 하위 플롯이 있는데, 두 개는 비디오 피드의 프레임을 표시하고 세 번째는 계산 결과를 막대 그래프로 표시합니다. matplotlib 그림을 만든 후 몇 가지 subplot2grid를 만든 다음 FuncAnimation으로 업데이트합니다. 나는 (업데이트 예정) 막대 그래프를 만들 것subplot2grid의 막대 그래프

일반적인 방법은 다음과 같습니다

fig = plt.figure() 
ax = plt.axes(xlim=(0, 9), ylim=(0, 100)) 
rects = plt.bar(res_x, res_y, color='b') 

def animate(args): 
    ... 
    ... 
    for rect, yi in zip(rects, results): 
     rect.set_height(yi*100) 
    return rects 

anim = animation.FuncAnimation(fig, animate, frames=200, interval=20, blit=True) 
plt.show() 
지금은 측면을 따라 막대 그래프 다른 줄거리 추가하려고

:

fig = plt.figure() 
plt1 = plt.subplot2grid((2, 2), (0, 0), rowspan=2) 
plt2 = plt.subplot2grid((2, 2), (0, 1)) 

#Confusion with the following 
bar_plot = plt.subplot2grid((2,2), (1,1)) 
ax = plt.axes(xlim=(0, 9), ylim=(0, 100)) 
rects = plt.bar(res_x, res_y, color='b') 


def animate(args): 
    ... 
    ... 

    im1 = plt1.imshow(...) 
    im2 = plt2.imshow(...) 

    for rect, yi in zip(rects, results): 
     rect.set_height(yi*100) 
    return im1, im2, rects 

anim = animation.FuncAnimation(fig, animate, frames=200, interval=20, blit=True) 
plt.show() 

I을 다음과 같은 오류를 얻을 : AttributeError를 'BarContainer'개체가

어떤 아이디어 'set_animated'에는 속성이없는 내가 어떻게 할 수있는 "장소"는 부가 적 줄거리로 막대 그래프, 그리고 다른 데이터 FR과 함께 업데이트해야 om subplots?

감사합니다.

답변

1

오류는 줄 return im1, im2, rects에서 발생합니다.

작업 솔루션을 사용하는 동안 return rects, 즉 set_animated 방법이있는 아티스트의 목록을 반환합니다. 실패한 코드에는 하나의 BarContainer와 두 명의 아티스트가 결합 된 튜플이 있습니다. 오류가 암시 하듯이 AttributeError : 'BarContainer'개체에 'set_animated'속성이 없습니다..

해결책은 다른 두 아티스트에게 연결할 수있는 BarContainer의 내용 목록을 만드는 것일 수 있습니다.

return [rect for rect in rects]+[im1, im2] 

전체 작업 예 :이 질문에 응답하면

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

res_x, res_y = [1,2,3], [1,2,3] 

fig = plt.figure() 
ax = plt.subplot2grid((2, 2), (0, 0), rowspan=2) 
ax2 = plt.subplot2grid((2, 2), (0, 1)) 
ax3 = plt.subplot2grid((2,2), (1,1)) 

rects = ax3.bar(res_x, res_y, color='b') 
im1 = ax.imshow([[1,2],[2,3]], vmin=0) 
im2 = ax2.imshow([[1,2],[2,3]], vmin=0) 

def animate(i): 

    im1.set_data([[1,2],[(i/100.),3]]) 
    im2.set_data([[(i/100.),2],[2.4,3]]) 

    for rect, yi in zip(rects, range(len(res_x))): 
     rect.set_height((i/100.)*(yi+0.2)) 
    return [rect for rect in rects]+[im1, im2] 

anim = animation.FuncAnimation(fig, animate, frames=200, interval=20, blit=True) 
plt.show() 
+0

@dtam, 당신은 [승인] 고려해야한다 (https://meta.stackexchange.com/questions/5234/how-does 대답하는 일). 그렇지 않은 경우 질문을 업데이트하여 자세한 내용을 제공 할 수 있습니다. 대답이 도움이된다면 상향 투표를 고려할 수도 있습니다 (문제가 해결된다면 아마도 그럴 것입니다). 물론, 이전 질문 ([여기] (http://stackoverflow.com/questions/43099734/combining-cv2-imshow-with-matplotlib-plt-show-in-real-time)과 [여기 ] (http://stackoverflow.com/questions/43372792/matplotlib-opencv-image-subplot)). – ImportanceOfBeingErnest

+0

이 솔루션을 사용하면 다음 오류가 표시됩니다. "AttributeError : draw_artist는 렌더링을 캐시하는 초기 그리기 후에 만 ​​사용할 수 있습니다." 또한 막대 그래프의 플롯이 열리지 만 다른 두 서브 플롯은 포함되지만 세 명 모두 하위 계보가되고 함께 모이기를 원합니다. 어떤 생각? 나는 이전의 것을 받아들이지 않는 것에 대해 사과합니다. 나는 지금 그들에게 연락 할 것입니다. – dtam

+0

전체 코드를 표시하지 않으므로이 오류의 출처를 알 수 없습니다. 하지만 나는 내 대답에 [mcve]를 추가하여 예상대로 작동 함을 보여줍니다. – ImportanceOfBeingErnest