{"id":1706,"date":"2015-06-04T23:06:50","date_gmt":"2015-06-04T22:06:50","guid":{"rendered":"http:\/\/www.2diabolos.com\/blog\/?page_id=1706"},"modified":"2024-08-26T19:46:29","modified_gmt":"2024-08-26T18:46:29","slug":"matplotlib-plot-refresh-time-optimization","status":"publish","type":"page","link":"https:\/\/www.2diabolos.com\/blog\/python-and-spyder\/matplotlib-plot-refresh-time-optimization\/","title":{"rendered":"matplotlib plot refresh time optimization"},"content":{"rendered":"<div class=\"columns\"><p>In my \u00ab\u00a0WebcamProfile\u00a0\u00bb application, I plotted lines after each webcam refresh.<\/p>\n<p>I first directly use the matplotlib <strong><em>plot<\/em><\/strong> function, and \u00ab\u00a0draw\u00a0\u00bb (function from the matplotlib widget). Note the \u00ab\u00a0hold\u00a0\u00bb function which \u00ab\u00a0clears\u00a0\u00bb the drawing if argument is False (j=0 in my code), and keep the previous plots if argument is True.<\/p>\n<p>[pastacode lang=\u00a0\u00bbpython\u00a0\u00bb message=\u00a0\u00bb\u00a0\u00bb highlight=\u00a0\u00bb\u00a0\u00bb provider=\u00a0\u00bbmanual\u00a0\u00bb]<\/p>\n<pre><code>for j in range(wideProfile.shape[1]):\r\n    self.mplwidget.axes.hold(j &gt; 0)\r\n    self.mplwidget.axes.plot(wideProfile[:, j],\r\n            color=myColorList[j])<\/code><\/pre>\n<p>[\/pastacode]<\/p>\n<p>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.<\/p>\n<p>I first asked google if there were a faster Python plotting package, and it seems that \u00ab\u00a0<a href=\"http:\/\/code.enthought.com\/projects\/chaco\/\">chaco<\/a>\u00a0\u00bb is meant for that. But I was not that motivated to learn a new plotting syntax&#8230; and I found a bit \u00ab\u00a0by accident\u00a0\u00bb a stackoverflow topic discussing about the matplotlib \u00ab\u00a0set_data\u00a0\u00bb function in order to just change plot line data withouth the need to re-create the complete ploting line.<\/p>\n<p>So I added some \u00ab\u00a0plot line declaration\u00a0\u00bb in my code like this (empty data, but color and line style definition)<\/p>\n<p>[pastacode lang=\u00a0\u00bbpython\u00a0\u00bb message=\u00a0\u00bb\u00a0\u00bb highlight=\u00a0\u00bb\u00a0\u00bb provider=\u00a0\u00bbmanual\u00a0\u00bb]<br \/>\nmyAxe.plot([], [], color=lineColors[j])[\/pastacode]<br \/>\nand in the main plotting \u00ab\u00a0loop\u00a0\u00bb (first code in this article) have been changed into this :<\/p>\n<p>[pastacode lang=\u00a0\u00bbpython\u00a0\u00bb message=\u00a0\u00bb\u00a0\u00bb highlight=\u00a0\u00bb\u00a0\u00bb provider=\u00a0\u00bbmanual\u00a0\u00bb]<\/p>\n<pre><code>for j in range(wideProfile.shape[1]):\r\n    self.mesPlots[j].set_data(xx,wideProfile[:, j])<\/code><\/pre>\n<p>[\/pastacode]<br \/>\nNote : no need to have any \u00ab\u00a0hold\u00a0\u00bb because we don&rsquo;t add new lines on the graphic (as plot does)<\/p>\n<p>Optimization factor : nearly a factor 2. The first code allow ~10 FPS whereas the 2nd syntax with \u00ab\u00a0set_data\u00a0\u00bb succeed to produce about 20 images.<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>In my \u00ab\u00a0WebcamProfile\u00a0\u00bb application, I plotted lines after each webcam refresh. I first directly use the matplotlib plot function, and \u00ab\u00a0draw\u00a0\u00bb (function from the matplotlib widget). Note the \u00ab\u00a0hold\u00a0\u00bb function which \u00ab\u00a0clears\u00a0\u00bb the drawing if argument is False (j=0 in my code), and keep the previous plots if argument is True. [pastacode lang=\u00a0\u00bbpython\u00a0\u00bb message=\u00a0\u00bb\u00a0\u00bb highlight=\u00a0\u00bb\u00a0\u00bb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":861,"menu_order":100,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-1706","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/1706","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/comments?post=1706"}],"version-history":[{"count":8,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/1706\/revisions"}],"predecessor-version":[{"id":1718,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/1706\/revisions\/1718"}],"up":[{"embeddable":true,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/861"}],"wp:attachment":[{"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/media?parent=1706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}