matplotlib plot refresh time optimization
In my « WebcamProfile » application, I plotted lines after each webcam refresh.
I first directly use the matplotlib plot function, and « draw » (function from the matplotlib widget). Note the « hold » function which « clears » the drawing if argument is False (j=0 in my code), and keep the previous plots if argument is True.
[pastacode lang= »python » message= » » highlight= » » provider= »manual »]
for j in range(wideProfile.shape[1]):
self.mplwidget.axes.hold(j > 0)
self.mplwidget.axes.plot(wideProfile[:, j],
color=myColorList[j])
[/pastacode]
My refresh rate was around 10fps with such a code. My webcam can actually much better, and I experienced it by removing the plot : ~30fps.
I first asked google if there were a faster Python plotting package, and it seems that « chaco » is meant for that. But I was not that motivated to learn a new plotting syntax… and I found a bit « by accident » a stackoverflow topic discussing about the matplotlib « set_data » function in order to just change plot line data withouth the need to re-create the complete ploting line.
So I added some « plot line declaration » in my code like this (empty data, but color and line style definition)
[pastacode lang= »python » message= » » highlight= » » provider= »manual »]
myAxe.plot([], [], color=lineColors[j])[/pastacode]
and in the main plotting « loop » (first code in this article) have been changed into this :
[pastacode lang= »python » message= » » highlight= » » provider= »manual »]
for j in range(wideProfile.shape[1]):
self.mesPlots[j].set_data(xx,wideProfile[:, j])
[/pastacode]
Note : no need to have any « hold » because we don’t add new lines on the graphic (as plot does)
Optimization factor : nearly a factor 2. The first code allow ~10 FPS whereas the 2nd syntax with « set_data » succeed to produce about 20 images.