- Undocumented Matlab - http://undocumentedmatlab.com -

Plot performance

Posted By Yair Altman On June 16, 2010 | 34 Comments

I recently consulted to a client who wanted to display an interactive plot with numerous data points that kept updating in real-time. Matlab’s standard plotting functions simply could not keep up with the rate of data change. Today, I want to share a couple of very simple undocumented hacks that significantly improve plotting performance and fixed my problem.
I begin by stating the obvious: whenever possible, try to vectorize [1] your code, preallocate the data [2] and other performance-improving techniques suggested by Matlab [3]. Unfortunately, sometimes (as in my specific case above) all these cannot help. Even in such cases, we can still find important performance tricks, such as these:

### Performance hack #1: manual limits

Whenever Matlab updates plot data, it checks whether any modification needs to be done to any of its limits. This computation-intensive task is done for any limit that is set to ‘Auto’ mode, which is the default axes limits mode. If instead we manually set the axes limits to the requested range, Matlab skips these checks, enabling much faster plotting performance.
Let us simulate the situation by adding 500 data points to a plot, one at a time:

```>> x=0:0.02:10; y=sin(x);
>> clf; cla; tic;
>> drawnow; plot(x(1),y(1)); hold on; legend data;
>> for idx = 1 : length(x); plot(x(idx),y(idx)); drawnow; end;
>> toc
Elapsed time is 21.583668 seconds.
```

And now let’s use static axes limits:

```>> x=0:0.02:10; y=sin(x);
>> clf; cla; tic; drawnow;
>> plot(x(1),y(1)); hold on; legend data;
>> xlim([0,10]); ylim([-1,1]);  % static limits
>> for idx = 1 : length(x); plot(x(idx),y(idx)); drawnow; end;
>> toc
Elapsed time is 16.863090 seconds.
```

Note that this trick is the basis for the performance improvement that occurs when using the plot’s undocumented set of LimInclude properties [4].
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. But if performance is important, we now know that we have this tool to improve it.

### Performance hack #2: static legend

Hack #1 gave us a 22% performance boost, but we can do much better. Running the profiler [5] on the code above we see that much of the time is spent recomputing the legend. 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:

```>> x=0:0.02:10; y=sin(x);
>> clf; cla; tic; drawnow;
>> plot(x(1),y(1)); hold on; legend data;
>> xlim([0,10]); ylim([-1,1]);  % static limits
>> % Static legend
>> set(gca,'LegendColorbarListeners',[]);
>> setappdata(gca,'LegendColorbarManualSpace',1);
>> setappdata(gca,'LegendColorbarReclaimSpace',1);
>> for idx = 1 : length(x); plot(x(idx),y(idx)); drawnow; end;
>> toc
Elapsed time is 5.209053 seconds.
```

Now this is much much better – a 76% performance boost compared to the original plot (i.e., 4 times faster!). Of course, it prevents the legend from being dynamically updated. Sometimes we actually wish for this dynamic effect (last year I explained how to use the legend’s undocumented -DynamicLegend feature [6] for even greater dynamic control). But when performance is important, we can still display a legend without its usual performance cost.
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.
Have you found other similar performance hacks? If so, please share them in the comments section [7] below.

Categories: Handle graphics, Low risk of breaking in future versions, Stock Matlab function, Undocumented feature

#1 Comment By Michelle Hirsch On June 16, 2010 @ 09:45

I agree with Perttu. Whenever I want performance, I manually set the xdata and ydata. This doesn’t cause any of the automagic stuff (like limit picking and legend setting) to happen.

My [14] on the file exchange shows this coding pattern, including tracking limits to make sure data isn’t cut off.

#2 Comment By Perttu Ranta-aho On June 16, 2010 @ 04:13

For comparison, your last code gives for me: 4.42 seconds, by replacing plot with line in for loop times to 4.0 seconds. In general line is more “raw” and thus faster than plot. However, nothing beats direct modification of the first plots x/ydata:

```x=0:0.02:10; y=sin(x);
clf; cla; tic; drawnow;
h=plot(x(1),y(1),'.','markersize',1); hold on; legend data;
xlim([0,10]); ylim([-1,1]);  % static limits
% Static legend
set(gca,'LegendColorbarListeners',[]);
setappdata(gca,'LegendColorbarManualSpace',1);
setappdata(gca,'LegendColorbarReclaimSpace',1);

for idx = 1 : length(x); set(h,'xdata',x(1:idx),'ydata',y(1:idx)); drawnow; end;
toc
% Elapsed time is 1.142915 seconds.
```

But obviously the result is not the same, since there is now only one line object instead of 501 that your code creates.

#3 Comment By Quentin On June 16, 2010 @ 09:36

I have had similar experiences to the previous commenter. For updating an existing plot, it’s always much faster to modify the properties of an existing object than delete old ones and create new ones (as is done in repeated calls to plot). I would also add that in a large GUI with many graphics objects, calls to “gca”, “gco” and “gcf” start to slow down considerably, so it’s always better to store the handles in advance and avoid calling any of those functions to get the handles, particularly for operations that are being done repeatedly inside loops.

#4 Comment By Yuri On August 31, 2010 @ 01:56

Yair,

First off – thanks.

Second – did you ever happen to need to “plot” icons instead of the usual circle/triangle markers?

I need to show icons (e.g. picture of a house, or a car) on the axes that behave like markers: not change size on zoom, and stay at the same axes coordinates.

Could you suggest how to do that?

#5 Comment By Yair Altman On August 31, 2010 @ 14:00

@Yuri – I have never tried to use icons as markers. You can control the images by designing a simple zoom callback hook function, that gets invoked whenever the axes are zoomed:

```hZoom = zoom(hFig);
hZoom.ActionPostCallback = @cbZoom;
```

Within your cbZoom callback function, simply reset all your marker images to the requested size.

#6 Comment By Nate On November 1, 2010 @ 09:40

Yair,
Don’t feel like you have to divulge all your secretes but I have to ask how do you find all these all these undocumented listeners and events? For instance I was recently trying to create a listener for when someone plotted on an axes and it was only after googling pretty extensively that I found the event that I needed to look for but could not find any information on how that person knew that the ‘ObjectChildAdded’ event existed. The same goes for the ‘LegendColorbarListeners’ property you use above. Is there anyway to find this information myself so I don’t have to bother you with unending questions? I tried using findjobj and uiinspect but didn’t see how to get the information from there.

Thanks,
Nate

#7 Comment By Yair Altman On November 1, 2010 @ 12:46

@Nate – there are many sources to my knowledge: I learn from posts by other people on the CSSM newsgroup; I learn from publicly-available Matlab m-code (much of the Matlab code-based resides in regular editable m-files, including all the legend code); I learn from tools like [15], [16] and [17]; I learn from other people who are gracious enough to report stuff to me; and I learn a lot by simple trial and error. There is never one single way.

I am not doing anything special that any other Matlab developer can’t do. But over the past years I have gathered lots of information and this helps me to more easily extrapolate where to look for answers, faster perhaps than most people. Still, I sometimes look for an answer to a particular question for many months, and I still do not know the answer to many things. There is still much left to be learned.

p.s. – here’s [18] to the same question, by U(r)s Schwartz, who is one of my information sources. This is actually one of my favorite posts on CSSM 🙂

-Yair

#8 Comment By Nate On November 1, 2010 @ 16:07

hahaha. That is quite the post. Well thanks for putting a lot of the information in one easy to use website. While there might not be any other easy shortcuts your knowledge definitely helps out.

Thanks,
Nate

#9 Comment By Nate On November 1, 2010 @ 16:21

P.S. Thanks for the hint about the legend code. It is a treasure trove.

#10 Comment By Helge On August 30, 2011 @ 18:43

(1) Don’t forget to set(hAxes,’DrawMode’,’fast’) whenever appropriate! This will tell the renderer NOT to check
which of the lines, patches etc need to be displayed on top of the others. Instead, Matlab will simply redraw
objects following the order in which they were created. I do not know whether DrawMode=’fast’ will have any effect
if we set(hg,’XData’,…) directly. However, as far as I understand, DrawMode=’fast’ will never slow down plotting.

(2) If I know that out-of-range data can never occur, I am usually setting the Clipping property so that Matlab
won’t waste any time to check whether data beyond xlim/ylim may need to be excluded from display.

(3) Finally, lots of automatic checking and property reset actions can be avoided by set(hAxes,’NextPlot’,’replacechildren’);

Whenever in doubt if legend updates might be an issue, I simply create a secondary axes, a2=copyobj(…),
draw some fake data points or lines outside xlim/ylim, create the legend once and set(a2,’Visible’,’off’);
Refreshing the data in the main (first) axes does then obviously not callback any of those nasty legend actions.

— btw: I did not run any performance tests for my suggestions, since actual performance improvement may vary
depending on the data/task. However, once again I found some great tips on this site. Thank to all of you!

H;

#11 Comment By Yair Altman On August 31, 2011 @ 00:26

@Helge – thanks for the useful additional tips 🙂

#12 Pingback By datestr performance | Undocumented Matlab On October 5, 2011 @ 13:22

[…] Different performance hotspots can have different solutions: caching, vectorization, internal library functions, undocumented graphics properties, smart property selection, smart function selection, smart indexing, smart parameter selection etc. […]

#13 Comment By Luke On November 16, 2011 @ 11:59

Not sure if this is obvious or not what you’re looking for, but I’m fairly certain the fastest way to plot is to plot to an invisible figure.

Here are my timings for your three:

```Elapsed time is 15.002303 seconds.
Elapsed time is 13.848813 seconds.
Elapsed time is 4.799191 seconds.```

And for Perttu’s:

`Elapsed time is 1.375230 seconds.`

Now with a minor modification to your original code:

```x=0:0.02:10; y=sin(x);
clf; cla; tic;
drawnow; plot(x(1),y(1)); set(gcf,'Visible','off'); hold on; legend data;
for idx = 1 : length(x); plot(x(idx),y(idx)); drawnow; end; set(gcf,'Visible','on');
toc```
`Elapsed time is 0.647195 seconds.`

#14 Comment By shabbychef On October 5, 2012 @ 11:25

would it not be more efficent to remove the drawnow from the loop and put it at the end? like so:

```x=0:0.02:10; y=sin(x);
clf; cla; tic;
drawnow; plot(x(1),y(1)); set(gcf,'Visible','off'); hold on; legend data;
for idx = 1 : length(x);
plot(x(idx),y(idx));
end;
drawnow; set(gcf,'Visible','on');
toc```

#15 Comment By Yair Altman On October 10, 2012 @ 12:08

@ShabbyChef – indeed, but then you would not see the effect on the legend. My point in this article was simply to illustrate ways in which plot legends can be optimized. Naturally, plotting in an invisible figure and moving drawnow out of the loop would improve the plotting performance, as well as using the vectorized version of the plot function. But again, I wanted to illustrate the point about the legend, which is why I used a simplistic sub-optimal plotting loop.

#16 Comment By Sabina On December 28, 2011 @ 01:38

Hello !
First of all thank you for all the tips you posted here!

I have the same problem with an interactive plot, a real time plot. I solved the problem but i wonder, is there a way to include this plot into a GUI? i can’t include the plot into a GUI figure, it always opens a new figure.

#17 Comment By Yair Altman On December 28, 2011 @ 12:44

@Sabina – the following links may help you: [19], [20]. If you still need my help, consider hiring my consulting services by sending me an email using the link at the top-right of this webpage.

#18 Pingback By A circular reference » Real Time Plotting with Matlab On August 21, 2012 @ 10:14

[…] It is also faster to have calculate the X and Y scales statically instead of using auto […]

#19 Comment By Ippei Kotera On November 30, 2012 @ 18:52

Hi Yair,

Thanks for the useful suggestion and other stuff elsewhere. You have helped me a lot!

Anyway, by combining your suggestion and Perttu’s along with ‘EraseMode’ = ‘none’ trick, the plotting speed could be boosted little further.

```x=0:0.02:10; y=sin(x);
clf; cla; tic; drawnow;
h=line(x(1),y(1)); hold on; legend data;
set(h,'EraseMode','none');
xlim([0,10]); ylim([-1,1]);  % static limits
% Static legend
set(gca,'LegendColorbarListeners',[]);
setappdata(gca,'LegendColorbarManualSpace',1);
setappdata(gca,'LegendColorbarReclaimSpace',1);

for idx = 1 : length(x); set(h,'xdata',x(idx),'ydata',y(idx)); drawnow; end;
toc
```
```Elapsed time is 0.469131 seconds.
```

But now the legend is little messed up. I wonder how you would fix that without compromising the speed.

#20 Comment By Yair Altman On December 1, 2012 @ 08:51

@Ippei – you could set up a single-shot timer that will run after some non-zero time delay, fixing the legend. This will enable your main function to process unhindered, and the legend will be updated a bit later, seemingly in parallel. In practice the timer would still use the main Matlab thread so the total run time would not improve, but the perceived performance would improve since the main bulk of you function would end sooner.

Alternately, you could set up the legend only after the main function ends, providing similar improved perceived performance although the total run-time will remain unchanged.

#21 Comment By cuisinart On March 5, 2013 @ 18:45

Thank you for the EraseMode suggestion. I am plotting in a GUI using the set data directly to YData (3 plots), while it worked OK I could only update the graph at about 12 Hz and still use 50% of a core. Now with EraseMode of the plots’ lines set to ‘background’, I can update the plots at a full 30 Hz and only use 4% of a core. HUGE difference.

thanks

#22 Pingback By Handle Graphics Behavior | Undocumented Matlab On March 7, 2013 @ 15:27

[…] This can be very important for plot performance, since the legend would not need to be updated whenever these objects are modified […]

#23 Comment By Chandrakanth On April 26, 2013 @ 00:47

Won’t replacing xdata and ydata using ‘set’ make it faster than using plot command? When I replaced plot function with the following

`set(p,'XData',x(1:idx),'YData',y(1:idx))`

I see 25% improvement. Did i miss something?

#24 Comment By Yair Altman On April 26, 2013 @ 02:45

@Chandrakanth – indeed so, and this was already pointed-out by Perttu, Scott and others. However, my point in the article was to use static axes limits, and I just used the loop to show that it runs faster with static axes limits. We need to compare apples to apples, which is why I used exactly the same plot-update loop. It will also run faster with static limits if you change the loop to set the xdata/ydata properties rather than re-plot. So going back to your example: yes, `set(p,'xdata',...)` is faster, but it will be even faster if you use it with the static axes limits that I have shown (and even faster with a few other tricks).

#25 Comment By Alessandro On November 26, 2014 @ 14:22

Thank you very much. I really enjoyed this topic!

#26 Comment By chfakht On February 7, 2015 @ 17:10

i want to use matlab for REAL TIME plotting from the serial port …
When i run it in matlab , it runs very well , but it stops after about 47 sec , and every time i re-execute it it stops again in something like 47 sec …

i have improved the performance by doing what you said but without any improvement
what will be the problem

#27 Comment By Yair Altman On February 8, 2015 @ 01:59

@chfakht – I assume that there is some programmatic or algorithmic bug in your program. From what you describe, it seems that the problem is not in the code performance (speed), but rather in the code correctness. If you’d like me to help you debug your program, contact me by email for a consulting offer.

#28 Pingback By Performance-Tweaks beim Plotten mit MATLAB | Stefan Grandl On March 28, 2015 @ 05:55

[…] […]

#29 Comment By Alex Perrone On January 22, 2016 @ 17:32

Great post. How can I implement static legend in 2014b? Specifically, given the axes handle, it doesn’t let me implement your trick:

```set(my_ax, 'LegendColorbarListeners',[]);  % my_ax is the axes where the legend is
Error using matlab.graphics.axis.Axes/set
There is no LegendColorbarListeners property on the Axes class.
```

However, if I change it to setappdata , that works: setappdata(my_ax, ‘LegendColorbarListeners’,[]). I can also do the other two calls with setappdata, but they don’t seem to help performance at all. I set them before and after making the legend and also at beginning of my ‘update’ function, to no avail — still a whole bunch of slow legend updates. Any ideas to make static legend in 2014b?

#30 Comment By Yair Altman On January 23, 2016 @ 19:32

@Alex – you can try the following in R2014b onward:

`my_ax.AutoListeners__ = {};`

Note that there are less risky alternatives for disabling legend for specific plot lines, for example:

`hasbehavior(hPlotLine,'legend',false);`

as described here: [21]

#31 Comment By Alex Perrone On January 26, 2016 @ 02:16

Thanks Yair, appreciate the response. I had a bug in setting static axis limits for the plot. I was not setting them on the plot with the legend (but rather another plot mistakenly). I set static limits by manually calculating my own max and min for x- and y-values. Once this was corrected, this got rid of all the legend update calls. It reduced the number of function calls as enumerated by profile(‘info’) from about 90 to 5, and time to update the plot (using set on XData and YData in all cases) from ~0.20 seconds to ~0.02 seconds, a 10x improvement. Persistence! Thanks, Yair.

#32 Comment By thanh On February 2, 2017 @ 18:27

dear all;

I’m working in my project and I want to draw 2 line in one axis, so I want to use set() function but I don’t know how to code, Anyone had this problem please let me know.

#33 Comment By Kavya M Managundi On November 22, 2018 @ 21:18

I’m trying to set the static axis and legends in app designer where I have a startup callback function in which I’m trying to read the COM port and when the terminator is reached a bytes available callback function has a imagesc to display the data. The x and y axis both are static and also the colormap and colorbar for imagesc, so I want to make them static but I don’t how exactly to do this in app designer. Because most of the examples are provided for GUIDE. Thanks for your time and effort! I would like to know if there are any similar documents for app designer or any link in which such slow interface problem in app designer is addressed.

#34 Comment By Yair Altman On November 27, 2018 @ 21:35

@Kavya – I’m afraid that the slowness of uifigures (created by App-Designer) is due to inherent limitations in the uifigures, not to the dynamic/static nature of the colorbar or legend. It’s true that static colorbars/legend will improve the speed a bit, but this will most probably be overshaddowed by uifigure’s underlying slowness.

MathWorks is well aware of the slowness of uifigures and are actively improving this aspect in each new Matlab release, so upgrading to the latest Matlab release might help. However, note that uifigures are slower than Java-based figures even today (R2018b). Hopefully this performance gap will finally be closed in an upcoming Matlab release.

URL to article: http://undocumentedmatlab.com/articles/plot-performance

URLs in this post:

[1] vectorize: http://www.mathworks.com/support/tech-notes/1100/1109.html

[2] preallocate the data: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f8-784135.html

[3] performance-improving techniques suggested by Matlab: http://www.mathworks.com/support/solutions/en/data/1-15NM7/

[4] undocumented set of LimInclude properties: http://undocumentedmatlab.com/blog/plot-liminclude-properties/

[5] profiler: http://undocumentedmatlab.com/blog/undocumented-profiler-options/

[6] legend’s undocumented -DynamicLegend feature: http://undocumentedmatlab.com/blog/legend-semi-documented-feature/

[8] Performance: scatter vs. line : http://undocumentedmatlab.com/articles/performance-scatter-vs-line

[9] Performance: accessing handle properties : http://undocumentedmatlab.com/articles/performance-accessing-handle-properties

[10] Plot LimInclude properties : http://undocumentedmatlab.com/articles/plot-liminclude-properties

[11] Bar plot customizations : http://undocumentedmatlab.com/articles/bar-plot-customizations

[12] Plot legend title : http://undocumentedmatlab.com/articles/plot-legend-title

[13] Undocumented scatter plot behavior : http://undocumentedmatlab.com/articles/undocumented-scatter-plot-behavior

[14] : http://www.mathworks.com/matlabcentral/fileexchange/4539-spectrum-scope

[15] : http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methods-properties-callbacks-of-an-object

[16] : http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/

[17] : http://www.mathworks.com/matlabcentral/fileexchange/26947-checkclass