Matplotlib animation with PyQt4
The code is fairly self-explanatory. We create a single line in the figure and then update it on every timer tick.
Edit: The code below is inspired by/borrowed from the new Matplotlib book by Sandro Tosi.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import sys import random from PyQt4 import QtGui from matplotlib.figure import Figure from matplotlib.backends.backend_qt4agg \ import FigureCanvasQTAgg as FigureCanvas class Monitor(FigureCanvas): def __init__(self): self.fig = Figure() self.ax = self.fig.add_subplot(111) # initialize the figure canvas FigureCanvas.__init__(self, self.fig) # set up the display limits for the figure self.ax.set_xlim(0,30) self.ax.set_ylim(0,100) # turn off autoscaling self.ax.set_autoscale_on(False) # this is the value that will be updated self.value = [] # this is the line that will be animated self.line_value, = self.ax.plot([], self.value, label="Values") # shwo the legend self.ax.legend() self.fig.canvas.draw() # start the Qt timer self.timerEvent(None) self.timer = self.startTimer(1000) def timerEvent(self, evt): """ This code will be executed on every timer tick. """ # get the new data value self.value.append(random.randrange(0,30)) # update the line with the new values self.line_value.set_data(range(len(self.value)), self.value) # force the redraw of the canvas self.fig.canvas.draw() if __name__ == "__main__": # set up the qt application app = QtGui.QApplication(sys.argv) # initialize the widget w = Monitor() # set the title of the window w.setWindowTitle('Updating in real time') # show the widged on the screen w.show() # start the main application loop sys.exit(app.exec_()) |
Did you, by any chance, take this code from my book: www.packtpub.com/matplotlib-python-development/book ? it looks damn similar to the real-time update of chapter 6.. same style, same code (changing the comments doesn’t change the “core” that much”)…
It’s not a problem per se, but if you use someone else’s code, a reference is in order. This way it seems the code is yours…
Comment by Sandro Tosi — Dec 1, 2009 3:00:07 PM | # - re
You’re right. I forgot to insert the reference.
Comment by Petro — Dec 1, 2009 4:04:00 PM | # - re
By the way, thanks for a great book.
Comment by Petro — Dec 1, 2009 4:07:46 PM | # - re
You’re welcome! let’s hope planet python pick up the new/edited post ;)
Comment by Sandro Tosi — Dec 1, 2009 6:00:18 PM | # - re
of course it didn’t :( do you mind reposting it and/or post a book review too? TIA
Comment by Sandro Tosi — Dec 2, 2009 1:13:53 AM | # - re
The edited version was picked up by Planet Python. I might write a quick review of the book if I get have time.
Comment by Petro — Dec 2, 2009 2:20:03 AM | # - re
yeah, I just saw it :) Yeah, it would be really welcome: readers opinion is really important for me!
Comment by Sandro Tosi — Dec 2, 2009 4:03:19 AM | # - re