plot legend -> bad performance

25 views (last 30 days)
Mari
Mari on 9 Oct 2013
Edited: Mari on 13 Oct 2013
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
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?

Sign in to comment.

Answers (2)

Yair Altman
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:
  1. Make the legend static so that it does not get updated when you update the axes. See http://undocumentedmatlab.com/blog/plot-performance/
  2. 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.
  1 Comment
Mari
Mari on 13 Oct 2013
Thanks for your suggestions. I tried them, but they didn't work for me. 1.: A static legend didn't help, because when i start a new plot (hold off -> hold on), the legend is removed. The example shown above only works if the old lines are never cleared. 2.: Updating the XData and YData didn't work, because the number and position of my plots is dynamic. For example, I may have two plots in axes1 and one plot in axes2, but in the next frame it's three plots in axes2 and none in axes1...

Sign in to comment.


Mari
Mari on 13 Oct 2013
Edited: Mari on 13 Oct 2013
OK, after spending a couple of days on this problem I'm giving up. The Matlab legends are obviously not made for my application (Multiple, quickly changing plots with dynamical legends).
However, I found a satisfying solution for my problem: Instead of a legend, I use a listbox in my GUI. The colors of the entries can be easily changed using HTML code. As the listbox is completely independent from my axes, there are no performance problems.
Thanks everyone for your help!

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!