Clear Filters
Clear Filters

Why doesn't legend location have "center"

88 views (last 30 days)
Quite intriguing why 'Location' doesn't have a 'center' option.

Accepted Answer

OCDER on 24 Jul 2018
Good point. Maybe because almost everyone do not use legends in the middle of a graph. The graph should highlight your data, not the legend. If the legend is in the center, it's distracting. Thus, I think MathWorks is smart to NOT include a 'center' location for a legend to prevent bad graphs from being generated via their platform. If the center is the 'best' location, that is a valid location option for legend. Otherwise, 'north' and 'south' is the next best center location. Or you could manually set the position of the legend, as Adam has shown above.
OCDER on 24 Jul 2018
Edited: OCDER on 24 Jul 2018
I see, you do have a rare case of center legend usage. I guess circular plots are becoming popular, but I often see the legend shoved towards the side. Here are some examples :
It's best to ask multiple people to see which legend position makes more sense.
Julian Groß-Funk
Julian Groß-Funk on 15 Sep 2021
There are so many possible reasons to place the legend in the middle. Here's another one

Sign in to comment.

More Answers (2)

Adam Danz
Adam Danz on 24 Jul 2018
Edited: Adam Danz on 16 Feb 2023
Why questions are tough. However, if you're trying to center the legend,
% Assuming your units are 'normalized'
lh = legend(...);
lh.Position(1) = 0.5 - lh.Position(3)/2;
lh.Position(2) = 0.5 - lh.Position(4)/2;
Starting in MATLAB R2020b (release notes), centering a legend within axes is possible using TiledLayout.
fp = fimplicit(@(x,y) y.*sin(x) + x.*cos(y) - 1);
lgd = legend;
lgd.Layout.Tile = 1; % assign tile location
th = linspace(0,2*pi);
x = sin(th).*(5:8)';
y = cos(th).*(5:8)';
axis equal
lgd = legend;
lgd.Layout.Tile = 2; % assign tile location
  1 Comment
Zhangxi Feng
Zhangxi Feng on 24 Jul 2018
I didn't quite mean to ask "why" but OCDER did do his/her best at answering that part. Thanks for providing a way to put the legend in the middle. I just thought if the location can be any of north/west/south/east, it might as well have a middle option.

Sign in to comment.

laurent jalabert
laurent jalabert on 29 Dec 2020
Edited: laurent jalabert on 29 Dec 2020
It is just a remark :
sometimes, placing the legend in the center of a blank graph is useful.
Especially when having the same legend for 15 subplots, and considering the 16th as blank to put there the unique legend for all the other subplots. In that case, the number of necessary subplot (15) is increased by +1, and the need to place the legend in the center makes sense.
In the following example, I just generate 5 figures, each having subplots from 2 to 6, and a dummy graph in which the legend will appear. It is quite suitable for 4 parameters studies for example. It works for subplot > 1.
For subplot =1 (not shown), the legend on the dummy graph is too much on left side, but I guess it can be adapted by getting ax(fr).Position (position of the dummy subplot), and knowing its dimensions and position, find the center, and move the legend to this center.
However the legend itself has a size, here in 3 columns, with possibly many values. For 3 values, the size is not that big, so my code can still be OK.
Therefore toward a function that will automatically center the legend on a dummy subplot, there is still a lot of work to do ... but I wanted to share what I am using so far.
% font size
fs = 14;
MMarkers = {'o','s','v','>','^','<','d','o','s','p','h','v','^','d'};
screensize = get( groot, 'Screensize' );
horiz = screensize(3); vertical = screensize(4);
FigList = findobj(allchild(0), 'flat', 'Type', 'figure');
nbfig = size(FigList,1); % find existing figures
nb_figure = 1; % if you have several variables Y1, Y2, ... Yn
colorsT = jet(8); % you can use better colors like parula, linspecer, etc...
for nb_subplot = 2:6; % number of subplots for a third parameter Z
nb_subplot_tot = [nb_subplot, nb_subplot+1]; %for legend on dummy graph
sub_col = ceil(sqrt(nb_subplot_tot)); % subplot column
sub_lin = ceil(nb_subplot_tot./sub_col); % subplot row
total_sub_fig = sub_col.*sub_lin; % total number of subplot (it is an array)
STRTe = [300 310 320]; % legend ; it can be [300 310 320 330 340] to display 5 curves/subplot
for n=1:nb_figure
fig(nbfig+n+1) = figure('PaperUnits','inches','PaperType','A4','PaperOrientation',...
'landscape','Color',[1 1 1], 'OuterPosition',[1 1 horiz vertical]);
for fr=1:nb_subplot % subplot
set(0, 'CurrentFigure', fig(nbfig+n+1));
ax(fr) = subplot(sub_col(2),sub_lin(2),fr); % sub_col is an array including dummy graph
for te=1:length(STRTe) % legende
markerColour = colorsT(te,:);
x = 0:1:10;
y = te.*x.^2;
err_y = rand.*0.1.*y;
hold on; grid on;
% dummy graph
if fr == nb_subplot % if it is the last subplot
ax(fr).Position; % to get the position of the subplot
% example of complicated legend
htitle = get(h,'Title'); % set title and values inside the legend
set(htitle,'String',{strcat('Param4 =',num2str(1.54,'%.2f'),' unit4');'unit_{legend}'},'FontSize',fs); %title header of the legend
hpos = get(h,'Position');
% this is the dummy graph
axis off; legend on;
hposlast = get(hlast,'Position');legend off; % get factive legend position
if nb_subplot(1) < sub_col(1)*sub_lin(1)
else set(h,'Position',[hposlast(1)-0.1,hposlast(2)-0.1,hposlast(3),hposlast(4)],'FontSize',fs);
end % nb_subplot

Community Treasure Hunt

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

Start Hunting!