Legend Position on a plot

343 次查看(过去 30 天)
Robert
Robert 2011-7-27
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 个评论
Jan
Jan 2011-7-27
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
You can try the below link
https://stackoverflow.com/questions/5674426/how-can-i-customize-the-positions-of-legend-elements

请先登录,再进行评论。

采纳的回答

Jan
Jan 2011-7-27
"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)

更多回答(2 个)

Usjes
Usjes 2012-4-26
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 个评论
Duy Tang Hoang
Duy Tang Hoang 2019-7-9
编辑:Duy Tang Hoang 2019-7-9
It works for me. Thanks. By the way, you can try 'Location','Southeast' or 'Location','Southwest' and so on...
William Harwin
William Harwin 2020-5-19
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.

请先登录,再进行评论。


Robert
Robert 2011-7-27
Thanks for the help. I tried removing the quotation marks, but then I got the error:
??? Error using ==> set Parameter must be a non-empty string.
Error in ==> energySpecPlotLi7 at 71 set(h,'Location',rect)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by