2017-05-22 15 views
1

파이썬 애플리케이션에 실시간으로 여러 개의 서브 플로트를 플롯하려고합니다. 이상적으로, 각 서브 플롯에도 여러 줄을 그릴 수 있어야합니다. 그러나 여기서 간단히하기 위해 서브 플롯 당 한 줄을 가정합니다. 이렇게 효율적으로 (나는 빠른 구획을 찾고 있습니다), 저는 온라인 (https://taher-zadeh.com/speeding-matplotlib-plotting-times-real-time-monitoring-purposes/)에서 찾을 수있는 한 가지 예를 제 경우로 확장하려고합니다. 내 코드는 다음과 같습니다파이썬 - 마지막 서브 플로트 만 플로팅하는 서브 플로트로 blit

import time  
# for Mac OSX 
import matplotlib 
matplotlib.use('TkAgg') 
import matplotlib.pylab as plt 
import random 

def test_fps(use_blit=True): 

    ax1.cla() 
    ax1.set_title('Sensor Input vs. Time -') 
    ax1.set_xlabel('Time (s)') 
    ax1.set_ylabel('Sensor Input (mV)') 
    ax2.cla() 
    ax2.set_title('Sensor Input vs. Time -') 
    ax2.set_xlabel('Time (s)') 
    ax2.set_ylabel('Sensor Input (mV)') 
    ax3.cla() 
    ax3.set_title('Sensor Input vs. Time -') 
    ax3.set_xlabel('Time (s)') 
    ax3.set_ylabel('Sensor Input (mV)') 
    ax4.cla() 
    ax4.set_title('Sensor Input vs. Time -') 
    ax4.set_xlabel('Time (s)') 
    ax4.set_ylabel('Sensor Input (mV)') 

    plt.ion() # Set interactive mode ON, so matplotlib will not be blocking the window 
    plt.show(False) # Set to false so that the code doesn't stop here 

    cur_time = time.time() 
    ax1.hold(True) 
    ax2.hold(True) 
    ax3.hold(True) 
    ax4.hold(True) 

    x, y = [], [] 
    times = [time.time() - cur_time] # Create blank array to hold time values 
    y.append(0) 

    line1, = ax1.plot(times, y, '.-', alpha=0.8, color="gray", markerfacecolor="red") 
    line2, = ax2.plot(times, y, '.-', alpha=0.8, color="gray", markerfacecolor="red") 
    line3, = ax3.plot(times, y, '.-', alpha=0.8, color="gray", markerfacecolor="red") 
    line4, = ax4.plot(times, y, '.-', alpha=0.8, color="gray", markerfacecolor="red") 


    fig.show() 
    fig.canvas.draw() 

    if use_blit: 
     background1 = fig.canvas.copy_from_bbox(ax1.bbox) # cache the background 
     background2 = fig.canvas.copy_from_bbox(ax2.bbox) # cache the background 
     background3 = fig.canvas.copy_from_bbox(ax3.bbox) # cache the background 
     background4 = fig.canvas.copy_from_bbox(ax4.bbox) # cache the background 

    tic = time.time() 

    niter = 200 
    i = 0 
    while i < niter: 

     fields = random.random() * 100 

     times.append(time.time() - cur_time) 
     y.append(fields) 

     # this removes the tail of the data so you can run for long hours. You can cache this 
     # and store it in a pickle variable in parallel. 

     if len(times) > 50: 
      del y[0] 
      del times[0] 

     xmin, xmax, ymin, ymax = [min(times)/1.05, max(times) * 1.1, -5,110] 

     # feed the new data to the plot and set the axis limits again 
     plt.axis([xmin, xmax, ymin, ymax]) 

     if use_blit: 
      fig.canvas.restore_region(background1) # restore background 
      line1.set_xdata(times) 
      line1.set_ydata(y) 
      ax1.draw_artist(line1)     # redraw just the points 
      fig.canvas.blit(ax1.bbox)    # fill in the axes rectangle 


      fig.canvas.restore_region(background2) # restore background 
      line2.set_xdata(times) 
      line2.set_ydata(y) 
      ax2.draw_artist(line2)     # redraw just the points 
      fig.canvas.blit(ax2.bbox) 

      fig.canvas.restore_region(background3) # restore background 
      line3.set_xdata(times) 
      line3.set_ydata(y) 
      ax3.draw_artist(line3)     # redraw just the points 
      fig.canvas.blit(ax3.bbox)   

      fig.canvas.restore_region(background4) # restore background 
      line4.set_xdata(times) 
      line4.set_ydata(y) 
      ax4.draw_artist(line4)     # redraw just the points 
      fig.canvas.blit(ax4.bbox) 

     else: 
      fig.canvas.draw() 

     fig.canvas.flush_events() 

     i += 1 

    fps = niter/(time.time() - tic) 
    return fps 

fig = plt.figure() 
ax1 = fig.add_subplot(4, 1, 1) 
ax2 = fig.add_subplot(4, 1, 2) 
ax3 = fig.add_subplot(4, 1, 3) 
ax4 = fig.add_subplot(4, 1, 4) 
fps1 = test_fps(use_blit=True) 

이 코드의 문제는 그 마지막 줄거리에 음모이며 공백으로 다른 사람을 잎.

enter image description here

나는 파이썬에 새로운 오전 나는 이것이 매우 어리석은 질문이다 그러나 나는 아직 그것을 알아 내기 위해 그렇게 모든 힌트는 나에게 매우 도움이 될 것입니다 관리하지 않은 것 같아요.

답변

0

현재 구현시 마지막 플롯에 대해서만 축 제한을 설정합니다. 즉, plt.axis([xmin, xmax, ymin, ymax])은 마지막 활성 서브 플로트에서 작동합니다.

대신 당신은

ax1.axis([xmin, xmax, ymin, ymax]) 
ax2.axis([xmin, xmax, ymin, ymax]) 
ax3.axis([xmin, xmax, ymin, ymax]) 
ax4.axis([xmin, xmax, ymin, ymax]) 

는 한계가 데이터를 따를 수 있도록, 모든 축 ax1 ax4로 업데이트해야합니다.

또한 블리 팅 조건 외의 데이터를 업데이트하여 블리 팅하지 않고 블리 팅을 비교할 수 있습니다.

전체 코드 :

import time  
import matplotlib 
matplotlib.use('TkAgg') 
import matplotlib.pylab as plt 
import random 

def test_fps(use_blit=True): 

    ax1.cla() 
    ax1.set_title('Sensor Input vs. Time -') 
    ax1.set_xlabel('Time (s)') 
    ax1.set_ylabel('Sensor Input (mV)') 
    ax2.cla() 
    ax2.set_title('Sensor Input vs. Time -') 
    ax2.set_xlabel('Time (s)') 
    ax2.set_ylabel('Sensor Input (mV)') 
    ax3.cla() 
    ax3.set_title('Sensor Input vs. Time -') 
    ax3.set_xlabel('Time (s)') 
    ax3.set_ylabel('Sensor Input (mV)') 
    ax4.cla() 
    ax4.set_title('Sensor Input vs. Time -') 
    ax4.set_xlabel('Time (s)') 
    ax4.set_ylabel('Sensor Input (mV)') 

    plt.ion() # Set interactive mode ON, so matplotlib will not be blocking the window 
    plt.show(False) # Set to false so that the code doesn't stop here 

    cur_time = time.time() 
    # ax1.hold(True) 
    # ax2.hold(True) 
    # ax3.hold(True) 
    # ax4.hold(True) 

    x, y = [], [] 
    times = [time.time() - cur_time] # Create blank array to hold time values 
    y.append(0) 

    line1, = ax1.plot(times, y, '.-', alpha=0.8, color="gray", markerfacecolor="red") 
    line2, = ax2.plot(times, y, '.-', alpha=0.8, color="gray", markerfacecolor="red") 
    line3, = ax3.plot(times, y, '.-', alpha=0.8, color="gray", markerfacecolor="red") 
    line4, = ax4.plot(times, y, '.-', alpha=0.8, color="gray", markerfacecolor="red") 


    fig.show() 
    fig.canvas.draw() 

    if use_blit: 
     background1 = fig.canvas.copy_from_bbox(ax1.bbox) # cache the background 
     background2 = fig.canvas.copy_from_bbox(ax2.bbox) # cache the background 
     background3 = fig.canvas.copy_from_bbox(ax3.bbox) # cache the background 
     background4 = fig.canvas.copy_from_bbox(ax4.bbox) # cache the background 

    tic = time.time() 

    niter = 200 
    i = 0 
    while i < niter: 

     fields = random.random() * 100 

     times.append(time.time() - cur_time) 
     y.append(fields) 

     # this removes the tail of the data so you can run for long hours. You can cache this 
     # and store it in a pickle variable in parallel. 

     if len(times) > 50: 
      del y[0] 
      del times[0] 

     xmin, xmax, ymin, ymax = [min(times)/1.05, max(times) * 1.1, -5,110] 

     # feed the new data to the plot and set the axis limits again 
     ax1.axis([xmin, xmax, ymin, ymax]) 
     ax2.axis([xmin, xmax, ymin, ymax]) 
     ax3.axis([xmin, xmax, ymin, ymax]) 
     ax4.axis([xmin, xmax, ymin, ymax]) 

     line1.set_data(times, y) 
     line2.set_data(times, y) 
     line3.set_data(times, y) 
     line4.set_data(times, y) 

     if use_blit: 
      fig.canvas.restore_region(background1) # restore background 
      ax1.draw_artist(line1)     # redraw just the points 
      fig.canvas.blit(ax1.bbox)    # fill in the axes rectangle 

      fig.canvas.restore_region(background2) # restore background 
      ax2.draw_artist(line2)     # redraw just the points 
      fig.canvas.blit(ax2.bbox) 

      fig.canvas.restore_region(background3) # restore background 
      ax3.draw_artist(line3)     # redraw just the points 
      fig.canvas.blit(ax3.bbox)   

      fig.canvas.restore_region(background4) # restore background 
      ax4.draw_artist(line4)     # redraw just the points 
      fig.canvas.blit(ax4.bbox) 

     else: 
      fig.canvas.draw() 

     fig.canvas.flush_events() 

     i += 1 

    fps = niter/(time.time() - tic) 
    return fps 

fig = plt.figure() 
ax1 = fig.add_subplot(4, 1, 1) 
ax2 = fig.add_subplot(4, 1, 2) 
ax3 = fig.add_subplot(4, 1, 3) 
ax4 = fig.add_subplot(4, 1, 4) 
fps1 = test_fps(use_blit=True) 
print fps1 

그냥이 주목해야 할이 10fps에서 블리 팅하지 않고 내 컴퓨터에서 16 fps로 블리 팅으로 실행됩니다.

+0

이 솔루션은 저에게 효과적입니다. 대단히 감사합니다. 어쨌든, 제 PC에서 blit없이 20fps를 얻었고 blit으로 96fps를 얻었습니다. – jappoz92

+0

오, 정말 대단합니다. 시간이 있다면, 18 및 28 fps와 비교하기 위해 [내 대답] (http://stackoverflow.com/a/40139416/4124317)의 matplotlib 부분에서 얻는 프레임 율을 알고 싶습니다. 나 거기 도착 했어. – ImportanceOfBeingErnest

+0

글쎄, 나는 당신의 질문에 코멘트를 추가 할만한 충분한 평판이 없다. 어쨌든, 나는 대략있다. matplotlib 버전의 복사 붙여 넣기 코드를 실행하여 blit없이 13fps 및 blit으로 16fps. – jappoz92