フィルターのクリア

Legend Position on a plot

482 ビュー (過去 30 日間)
Robert
Robert 2011 年 7 月 27 日
コメント済み: William Harwin 2020 年 5 月 19 日
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
SIVAKUMAR KARURNKARAN 2018 年 4 月 8 日
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)
  3 件のコメント
Rotimi Agbebi
Rotimi Agbebi 2015 年 9 月 30 日
Thanks for this, made some progress
Md Borhan Mia
Md Borhan Mia 2018 年 5 月 21 日
Thanks :)

サインインしてコメントする。

その他の回答 (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)
  1 件のコメント
Jan
Jan 2011 年 7 月 27 日
@Robert: See my edited former post.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by