animation - Animate a Histogram in Python -


i'm trying animate histogram on time, , far code have following one:

import matplotlib.pyplot plt import numpy np import time plt.ion() fig = plt.figure() ax = fig.add_subplot(111)  alphab = ['a', 'b', 'c', 'd', 'e', 'f'] frequencies = [1, 44, 12, 11, 2, 10]  pos = np.arange(len(alphab)) width = 1.0     # gives histogram aspect bar diagram ax.set_xticks(pos + (width / 2)) ax.set_xticklabels(alphab) bin_idx in np.linspace(0,1000000,100000000):      t = time.time()      #here change first bin, increases through animation.      frequencies[0] = bin_idx      line1 =plt.bar(pos, frequencies, width, color='r')      plt.draw()      elapsed = time.time() - t      print elapsed 

the code works, outputs shows how after iterations becomes way slower @ beginning. there way speed things up, want update in real time, , process in runs pretty fast.

also, important notice, not want post processing animation, want real time updates, histogram animation example not working particular process.

thanks

if have newer version of matplotlib there animations.funcanimation class can reduce of boiler-plate code. (see page example.) pretty fast (~ 52 frames per second):

import numpy np import matplotlib.pyplot plt import matplotlib.animation animation import timeit  clock = timeit.default_timer  fig, ax = plt.subplots()  alphab = ['a', 'b', 'c', 'd', 'e', 'f'] frequencies = [1, 44, 12, 11, 2, 10]  pos = np.arange(len(alphab)) width = 1.0     # gives histogram aspect bar diagram ax.set_xticks(pos + (width / 2)) ax.set_xticklabels(alphab)  rects = plt.bar(pos, frequencies, width, color='r') start = clock()  def animate(arg, rects):     frameno, frequencies = arg     rect, f in zip(rects, frequencies):         rect.set_height(f)     print("fps: {:.2f}".format(frameno / (clock() - start)))   def step():     frame, bin_idx in enumerate(np.linspace(0,1000000,100000000), 1):         #here change first bin, increases through animation.         frequencies[0] = bin_idx         yield frame, frequencies   ani = animation.funcanimation(fig, animate, step, interval=10,                               repeat=false, blit=false, fargs=(rects,)) plt.show() 

if don't have newer version of matplotlib, here older way it. slower (~ 45 frames per second):

don't call plt.bar each iteration of loop. instead, call once, save rects return value, , call set_height modify height of rects on subsequent iterations of loop. trick (and others) explained in matplotlib animations cookbook.

import sys import matplotlib mpl mpl.use('tkagg')  # before importing pyplot import matplotlib.pyplot plt import numpy np import timeit  clock = timeit.default_timer  fig, ax = plt.subplots()  alphab = ['a', 'b', 'c', 'd', 'e', 'f'] frequencies = [1, 44, 12, 11, 2, 10]  pos = np.arange(len(alphab)) width = 1.0     # gives histogram aspect bar diagram ax.set_xticks(pos + (width / 2)) ax.set_xticklabels(alphab)  def animate():     start = clock()     rects = plt.bar(pos, frequencies, width, color='r')     frameno, bin_idx in enumerate(np.linspace(0,1000000,100000000), 2):         #here change first bin, increases through animation.         frequencies[0] = bin_idx         # rects = plt.bar(pos, frequencies, width, color='r')         rect, f in zip(rects, frequencies):             rect.set_height(f)         fig.canvas.draw()         print("fps: {:.2f}".format(frameno / (clock() - start)))           win = fig.canvas.manager.window win.after(1, animate) plt.show() 

for comparison, adding plt.clf original code, on machine reaches 12 frames per second.


some comments timing:

you won't accurate measurements calculating small time differences each pass through loop. time resolution of time.time() -- @ least on computer -- not great enough. you'll more accurate measurements measuring 1 starting time , calculating large time difference between start time , current time, , dividing number of frames.

i changed time.time timeit.default_timer. 2 same on unix computers, timeit.default_timer set time.clock on windows machines. timeit.default_timer chooses more accurate timer each platform.


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -