I'm having issues with redrawing the figure here. I allow the user to specify the units in the time scale (x-axis) and then I recalculate and call this function plots()
. I want the plot to simply update, not append another plot to the figure.
def plots():
global vlgaBuffSorted
cntr()
result = collections.defaultdict(list)
for d in vlgaBuffSorted:
result[d['event']].append(d)
result_list = result.values()
f = Figure()
graph1 = f.add_subplot(211)
graph2 = f.add_subplot(212,sharex=graph1)
for item in result_list:
tL = []
vgsL = []
vdsL = []
isubL = []
for dict in item:
tL.append(dict['time'])
vgsL.append(dict['vgs'])
vdsL.append(dict['vds'])
isubL.append(dict['isub'])
graph1.plot(tL,vdsL,'bo',label='a')
graph1.plot(tL,vgsL,'rp',label='b')
graph2.plot(tL,isubL,'b-',label='c')
plotCanvas = FigureCanvasTkAgg(f, pltFrame)
toolbar = NavigationToolbar2TkAgg(plotCanvas, pltFrame)
toolbar.pack(side=BOTTOM)
plotCanvas.get_tk_widget().pack(side=TOP)
You essentially have two options:
Do exactly what you're currently doing, but call
graph1.clear()
andgraph2.clear()
before replotting the data. This is the slowest, but most simplest and most robust option.Instead of replotting, you can just update the data of the plot objects. You'll need to make some changes in your code, but this should be much, much faster than replotting things every time. However, the shape of the data that you're plotting can't change, and if the range of your data is changing, you'll need to manually reset the x and y axis limits.
To give an example of the second option:
You can also do like the following: This will draw a 10x1 random matrix data on the plot for 50 cycles of the for loop.
In case anyone comes across this article looking for what I was looking for, I found examples at
How to visualize scalar 2D data with Matplotlib?
and
http://mri.brechmos.org/2009/07/automatically-update-a-figure-in-a-loop (on web.archive.org)
then modified them to use imshow with an input stack of frames, instead of generating and using contours on the fly.
Starting with a 3D array of images of shape (nBins, nBins, nBins), called
frames
.I also found a much simpler way to go about this whole process, albeit less robust:
Note that both of these only seem to work with
ipython --pylab=tk
, a.k.a.backend = TkAgg
Thank you for the help with everything.
I have released a package called python-drawnow that provides functionality to let a figure update, typically called within a for loop, similar to Matlab's
drawnow
.An example usage:
This package works with any matplotlib figure and provides options to wait after each figure update or drop into the debugger.
This worked for me. Repeatedly calls a function updating the graph every time.
"fun" is a function that returns an integer. FuncAnimation will repeatedly call "update", it will do that "xmax" times.
All of the above might be true, however for me "online-updating" of figures only works with some backends, specifically
wx
. You just might try to change to this, e.g. by starting ipython/pylab byipython --pylab=wx
! Good luck!