How to update the position of legend with 'Best' Location?
551 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I have an output function which I use with fmincon to plot some of variables during optimization process. I use
legend(...,'Location', 'Best')
to define the legend, and in the first iteration it looks really okay. However, as the optimization goes, I update my graph with:
set(h, 'YData', new_data);
And sometimes the legend overlaps with the new plot (it doesn't reposition to adjust for new data). Is there any way I could reposition automatically my legend (I guess apart from deleting it and constructing again every iteration)?
Cheers, Aleksander
3 comentarios
dpb
el 27 de Sept. de 2016
That's actually what I expected; legend is just an m-file and set on the properties after the fact only changes that string recorded in the 'location' property; it doesn't trigger any callbacks or such.
You'd have to use a callback function when data are updated and that's more trouble than simply re-issuing the legend command each iteration.
While no doubt it's not as neat in appearance, if the redrawing is distracting during the simulation, you could place the legend outside the axes so it won't occlude the data and then try the 'best' location at the end for final viewing.
Respuestas (1)
Massimo Zanetti
el 27 de Sept. de 2016
At every new plot reissue the command
legend(...,'Location', 'Best')
So that Matlab recomputes the right position to place it.
0 comentarios
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!