Legend Position on a plot
501 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Robert
el 27 de Jul. de 2011
Comentada: William Harwin
el 19 de Mayo de 2020
I'm trying to manually set the position of my legend in a plot that I am doing because the default locations do not work for me. The relevant portion of the code is:
h=legend('Nmax=8','Nmax=10','Nmax=12','Nmax=14'); set(h, 'Box', 'off') set(h,'fontsize',12,'fontweight','b') set(gcf,'Units','normalized') rect=[0.25, 0.25, .25, .25]; set(h,'Location','rect') print -depsc2 Li7.Nm14.EnergySpec.eps
I keep getting the following error message: ??? Error using ==> set The 'rect' enumerated value is invalid.
Error in ==> energySpecPlotLi7 at 71 set(h,'Location','rect')
I have tried several different values for the position vector, but I still can't seem to get it to work. Any idea what I am doing wrong?
Thanks in advance!
2 comentarios
Jan
el 27 de Jul. de 2011
Please use the "{} Code" button to format the code. Follow the "Markup" link on this page also to lear more about formatting a question to improve the readability.
SIVAKUMAR KARURNKARAN
el 8 de Abr. de 2018
You can try the below link
https://stackoverflow.com/questions/5674426/how-can-i-customize-the-positions-of-legend-elements
Respuesta aceptada
Jan
el 27 de Jul. de 2011
"set(h,'Location','rect')" uses the string 'rect', but you want to use the variable.
[EDITED]: The 'Location' property uses the strings 'southeast', 'northeast', 'best' etc. as argument. If you want to set the position to a rectangle, use the 'Position' property:
rect = [0.25, 0.25, .25, .25];
set(h, 'Position', rect)
3 comentarios
Más respuestas (2)
Usjes
el 26 de Abr. de 2012
Well this wasted an hour of my life but I finally worked the answer out for myself although I find it incredible that Mathworks has not documented this anyhwere, but anyway here goes:
Jan you are incorrect in saying that the 'Location' property is not used for specifying the legend location. The Matlab help for legend clearly states:
LEGEND(...,'Location',LOC) adds a legend in the specified
location, LOC, with respect to the axes. LOC may be either a
1x4 position vector or one of the following strings:
'North' inside plot box near top
'South' inside bottom
=> it should work by sepcifying a 4 element array along with the 'Location' property although infuriatingly the help does not specify the units used in this vector. From experimentation though it seems that the four element 'Location' vector has the format: ['Lefthand edge coordinate' 'Bottom Edge coordinate' 'width' 'height'] ... this much at least is specified in 'doc legend' however the crucial missing information is the units that these four numbers are measured in and through experimentation I have discovered that they seem to be fractions of the overall figure dimensions (and entirely unrelated to your axes scales) => for example 'Location' [0.9 0.9 0.1 0.1] will give you a legend box which is 1 tenth of the figure window height tall, 1 tenth of the fiugre window width wide and located at the extreme top right of the figure window.
So Jan, you may also be able to control the location of the legend by using the 'Position' property however the method specified in the help function is via the 'Location' property but the help fails spectacularly in explaining how exactly to use it.
3 comentarios
Duy Tang Hoang
el 9 de Jul. de 2019
Editada: Duy Tang Hoang
el 9 de Jul. de 2019
It works for me. Thanks. By the way, you can try 'Location','Southeast' or 'Location','Southwest' and so on...
William Harwin
el 19 de Mayo de 2020
Possilby simplist to set the legend with a handle
l=legend({'line1','line2','etc'})
you can then drag it to where you would like positioned and then read the position from l.Position, Next time you can use this position to put it back in that place automatically. You can then also use l to adjust columns fonts etc.
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!