Animated Bar Graphs with Matplotlib and PyQt4

The new Matplotlib book shows how to animate line drawing. What I really needed was bar graphs. I didn't see an example of animating bar graphs in the book (probably because it is follows naturally from the line animation example). The bar graph documentation for matplotib shows how to create a set of bar graphs. The code for animated bars is below.

import sys
from PyQt4 import QtGui

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

import numpy as np

class Monitor(FigureCanvas):
    def __init__(self):
        self.fig = Figure() = self.fig.add_subplot(111)

        FigureCanvas.__init__(self, self.fig)

        # the file is 5 float values separated by spaces on every line, like this:
        #0.28 0.18 0.18 0.18 0.18
        #0.352 0.162 0.162 0.162 0.162
        #0.4168 0.1458 0.1458 0.1458 0.1458
        #0.37512 0.23122 0.13122 0.13122 0.13122 = [np.array(map(float, line.strip().split())) for line in open('data.txt').readlines()]
        self.counter = 1

        # the width of the bars
        self.width = 0.8

        # the locations of the bars
        self.locs = np.arange(len([0]))

        # the first set of bars
        self.bars =,[0], self.width, color='#6a7ea6')

        # set up the lables for the bars['Red', 'Green', 'Black', 'Orange', 'Yellow'])

        # set the limit for the x and y, len([0])), 1.,)

        # draw the canvas

        # start the timer
        self.timer = self.startTimer(1000)

    def timerEvent(self, evt):
        # update the height of the bars, one liner is easier
        [bar.set_height([self.counter][i]) for i,bar in enumerate(self.bars)]

        # force the redraw of the canvas

        # update the data row counter
        self.counter += 1

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = Monitor()