Guest User

Untitled

a guest
May 24th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. import time
  2.  
  3. # for Mac OSX
  4. import matplotlib
  5. matplotlib.use('Qt5Agg')
  6.  
  7. import matplotlib.pylab as plt
  8. import random
  9.  
  10. # from mpltools import style
  11. # style.use('ggplot')
  12.  
  13. fig = plt.figure()
  14. ax1 = fig.add_subplot(1, 1, 1)
  15.  
  16. def test_fps(use_blit=True):
  17.  
  18.     ax1.cla()
  19.     ax1.set_title('Sensor Input vs. Time -' + 'Blit [{0:3s}]'.format("On" if use_blit else "Off"))
  20.     ax1.set_xlabel('Time (s)')
  21.     ax1.set_ylabel('Sensor Input (mV)')
  22.  
  23.     plt.ion()  # Set interactive mode ON, so matplotlib will not be blocking the window
  24.     plt.show(False)  # Set to false so that the code doesn't stop here
  25.  
  26.     cur_time = time.time()
  27.     ax1.hold(True)
  28.  
  29.     x, y = [], []
  30.     times = [time.time() - cur_time]  # Create blank array to hold time values
  31.     y.append(0)
  32.  
  33.     line1, = ax1.plot(times, y, '.-', alpha=0.8, color="gray", markerfacecolor="red")
  34.  
  35.     fig.show()
  36.     fig.canvas.draw()
  37.  
  38.     plt.axis([0, 10, 0, 100])
  39.  
  40.     if use_blit:
  41.         background = fig.canvas.copy_from_bbox(ax1.bbox) # cache the background
  42.  
  43.     tic = time.time()
  44.  
  45.     niter = 300
  46.     i = 0
  47.     while i < niter:
  48.  
  49.         fields = random.random() * 100
  50.         # fields = i
  51.  
  52.         times.append(time.time() - cur_time)
  53.         # times.append(i)
  54.         y.append(fields)
  55.  
  56.         # this removes the tail of the data so you can run for long hours. You can cache this
  57.         # and store it in a pickle variable in parallel.
  58.  
  59.         if len(times) > 50:
  60.            del y[0]
  61.            del times[0]
  62.  
  63.         xmin, xmax, ymin, ymax = [min(times) / 1.05, max(times) * 1.1, -5,110]
  64.  
  65.         # feed the new data to the plot and set the axis limits again
  66.         line1.set_xdata(times)
  67.         line1.set_ydata(y)
  68.  
  69.         plt.axis([xmin, xmax, ymin, ymax])
  70.  
  71.         if use_blit:
  72.             # fig.canvas.restore_region(background)    # restore background
  73.             # line1.set_data(times[-1], y[-1])
  74.             # ax1.draw_artist(line1)
  75.             # ax1.draw_artist(line1)                   # redraw just the points
  76.             # fig.canvas.blit(ax1.bbox)                # fill in the axes rectangle
  77.             # ax1.draw_artist(ax1.patch)
  78.             ax1.draw_artist(line1)
  79.             # fig.canvas.update()
  80.             if i % 1 == 0:
  81.                 fig.canvas.flush_events()
  82.             # plt.pause(0.001)
  83.         else:
  84.             fig.canvas.draw()
  85.  
  86.         i += 1
  87.  
  88.     fps = niter / (time.time() - tic)
  89.     print("Blit [{0:3s}] -- FPS: {1:.1f}, time resolution: {2:.4f}s".format("On" if use_blit else "Off", fps, 1/fps))
  90.     return fps
  91.  
  92. fps1 = test_fps(use_blit=True)
  93. # fps2 = test_fps(use_blit=False)
  94.  
  95. # print("-"*50)
  96. # print("With Blit ON plotting is {0:.2f} times faster.".format(fps1/fps2))
Advertisement
Add Comment
Please, Sign In to add comment