plot legend -> bad performance

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 comentarios

dpb
dpb el 9 de Oct. de 2013
Editada: dpb el 9 de Oct. de 2013
Because it's got a lot to do, maybe??? :)
Why redraw them from scratch every time; save the handles to the appropriate pieces and update them directly (like the text objects). What within the legends is actually changing every frame?
BTW, similar ideas probably would be good for the plotting as well--that is, just update the [x|y]data properties, don't redraw an entire plot.
There are examples in the doc's under "animation" that give details.
Mari
Mari el 9 de Oct. de 2013
If drawing four plots takes 35ms, drawing four simple legends should not take 160ms, it's really not that much to do... You're right, updating xdata and ydata should be faster, but at this point there are no performance problems with the plotting itself, only with the legends. I tried to create the legends once and then redraw it using the handle, but I couldn't figure out how this works...
dpb
dpb el 9 de Oct. de 2013
Well, there IS a lot going on in legend whether you think so or not -- it's having to do the lookup of the handles to associate the lines with and all that plus it's a whole new axes object. tic/toc don't lie in this case...
Again, which properties are you wanting to change? That determines the handle(s) you need.
Mari
Mari el 9 de Oct. de 2013
I have a dynamical number of line plots, that are drawn in four axes objects. Through the GUI, the user can select which line is to be shown in which axes. In theory, the whole plot (and legend) can change from one frame to the next. However, it would be enough to refresh the legend every 20th time or so. For all other frames, it is ok to redraw the old legend.
dpb
dpb el 9 de Oct. de 2013
Editada: dpb el 9 de Oct. de 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?

Iniciar sesión para comentar.

Respuestas (2)

Yair Altman
Yair Altman el 9 de Oct. de 2013

0 votos

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 comentario

Mari
Mari el 13 de Oct. de 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...

Iniciar sesión para comentar.

Mari
Mari el 13 de Oct. de 2013
Editada: Mari el 13 de Oct. de 2013

0 votos

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!

Productos

Preguntada:

el 9 de Oct. de 2013

Editada:

el 13 de Oct. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by