Borrar filtros
Borrar filtros

Legend Position on a plot

482 visualizaciones (últimos 30 días)
Robert
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
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
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

Iniciar sesión para comentar.

Respuesta aceptada

Jan
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
Rotimi Agbebi
Rotimi Agbebi el 30 de Sept. de 2015
Thanks for this, made some progress
Md Borhan Mia
Md Borhan Mia el 21 de Mayo de 2018
Thanks :)

Iniciar sesión para comentar.

Más respuestas (2)

Usjes
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
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
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.

Iniciar sesión para comentar.


Robert
Robert el 27 de Jul. de 2011
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 comentario
Jan
Jan el 27 de Jul. de 2011
@Robert: See my edited former post.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by