plot legend -> bad performance
22 views (last 30 days)
Show older comments
Hi!
I have a GUI with four axes-objects showing line plots, which I'd like to update about ten times per second. Every line has a 'DisplayName' property, so that I can draw the legends using
legend(handles.axes1,'show');
legend(handles.axes2,'show');
legend(handles.axes3,'show');
legend(handles.axes4,'show');
Everything works fine, as long as I don't use legends. But as soon as I draw the legends, performance gets sigificantly worse. My anaylsis using tic/toc shows that the whole plotting procedure without legends takes about 35ms, but the four legend commands shown above take about 160ms.
Does anybody have an idea why the legend command is so slow and how it can be accelerated?
5 Comments
dpb
on 9 Oct 2013
Edited: dpb
on 9 Oct 2013
Well, that would simply be keeping a counter modulo some N. But again, why would you redraw it every time; that wouldn't help?
Perhaps I don't understand what you're changing at such a frequency--the UI certainly can't be being stroked by a user at 10 Hz...actually, there's no "perhaps" here, it isn't at all clear to me from your description what's actually changing on the legend that means it needs updating so frequently.
I can envision every so often the user puts a different piece of data on a given axis, but it would seem that once that happens and the legends are updated to match they would be static until the next time they're changed?
Answers (2)
Yair Altman
on 9 Oct 2013
The legend is automatically connected to the axes so that whenever the axes children change (as in deleting/adding plot lines), the legend gets redrawn and this is quite a time-consuming operation.
You could do 2 things that are directly related to legend performance:
- Make the legend static so that it does not get updated when you update the axes. See http://undocumentedmatlab.com/blog/plot-performance/
- Do not delete/add lines but rather update the existing plot lines' properties (specifically their XData and YData). This will significantly improve performance even if you do not show a legend. But when you have a legend it helps by not forcing the legend to redraw.
There are other suggestions to improve plotting performance, but they do not directly relate to the legend.
See Also
Categories
Find more on Legend in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!