![]() Setappdata(gca,'LegendColorbarReclaimSpace',1) įor idx = 1 : length(x) set(h,'xdata',x(1:idx),'ydata',y(1:idx)) drawnow end īut obviously the result is not the same, since there is now only one line object instead of 501 that your code creates. Setappdata(gca,'LegendColorbarManualSpace',1) Setappdata ( gca, 'LegendColorbarReclaimSpace', 1 ) įor idx = 1 : length (x ) set (h, 'xdata',x ( 1:idx ), 'ydata',y ( 1:idx ) ) drawnow end Setappdata ( gca, 'LegendColorbarManualSpace', 1 ) Xlim ( ) ylim ( ) % static limits % Static legend set ( gca, 'LegendColorbarListeners', ) H= plot (x ( 1 ),y ( 1 ), '.', 'markersize', 1 ) hold on legend data Undocumented scatter plot behavior – The scatter plot function has an undocumented behavior when plotting more than 100 points: it returns a single unified patch object handle, rather than a patch handle for each specific point as it returns with 100 or less points.Plot legend title – Titles to plot legends are easy to achieve in HG1 (R2014a or earlier), but much more difficult in HG2 (R2014b or newer).Bar plot customizations – Matlab bar charts can be customized in various nifty ways.Plot LimInclude properties – The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications.Performance: accessing handle properties – Handle object property access (get/set) performance can be significantly improved using dot-notation. ![]() line – In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster. Have you found other similar performance hacks? If so, please share them in the comments section below. In conclusion, I have demonstrated that Matlab performance can often be improved significantly, even in the absence of any vectorization, by simply understanding the internal mechanisms and bypassing those which are irrelevant in our specific case. But when performance is important, we can still display a legend without its usual performance cost. Sometimes we actually wish for this dynamic effect (last year I explained how to use the legend’s undocumented -DynamicLegend feature for even greater dynamic control). Of course, it prevents the legend from being dynamically updated. ![]() Now this is much much better – a 76% performance boost compared to the original plot (i.e., 4 times faster!). > setappdata(gca,'LegendColorbarReclaimSpace',1) > setappdata(gca,'LegendColorbarManualSpace',1) > setappdata ( gca, 'LegendColorbarReclaimSpace', 1 ) > setappdata ( gca, 'LegendColorbarManualSpace', 1 ) > set ( gca, 'LegendColorbarListeners', ) Looking inside the legend code (specifically, the legendcolorbarlayout function), we detect several short-circuits that we can use to make the legend static and prevent recomputation: Running the profiler on the code above we see that much of the time is spent recomputing the legend. Hack #1 gave us a 22% performance boost, but we can do much better. But if performance is important, we now know that we have this tool to improve it. Of course, setting manual limits prevents the axes limits from growing and shrinking automatically with the data, which can actually be a very useful feature sometimes. Note that this trick is the basis for the performance improvement that occurs when using the plot’s undocumented set of LimInclude properties. > for idx = 1 : length(x) plot(x(idx),y(idx)) drawnow end > for idx = 1 : length (x ) plot (x (idx ),y (idx ) ) drawnow end > plot (x ( 1 ),y ( 1 ) ) hold on legend data Let us simulate the situation by adding 500 data points to a plot, one at a time: If instead we manually set the axes limits to the requested range, Matlab skips these checks, enabling much faster plotting performance. This computation-intensive task is done for any limit that is set to ‘Auto’ mode, which is the default axes limits mode. Whenever Matlab updates plot data, it checks whether any modification needs to be done to any of its limits. Even in such cases, we can still find important performance tricks, such as these: Performance hack #1: manual limits Unfortunately, sometimes (as in my specific case above) all these cannot help. I begin by stating the obvious: whenever possible, try to vectorize your code, preallocate the data and other performance-improving techniques suggested by Matlab. Today, I want to share a couple of very simple undocumented hacks that significantly improve plotting performance and fixed my problem. Matlab’s standard plotting functions simply could not keep up with the rate of data change. I recently consulted to a client who wanted to display an interactive plot with numerous data points that kept updating in real-time.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |