Why doesn't legend location have "center"

76 ビュー (過去 30 日間)
Zhangxi Feng
Zhangxi Feng 2018 年 7 月 24 日
編集済み: Adam Danz 2023 年 2 月 16 日
Quite intriguing why 'Location' doesn't have a 'center' option.

採用された回答

OCDER
OCDER 2018 年 7 月 24 日
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.
  3 件のコメント
OCDER
OCDER 2018 年 7 月 24 日
編集済み: OCDER 2018 年 7 月 24 日
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 : https://www.familytreetemplates.net/category/circle
It's best to ask multiple people to see which legend position makes more sense.
Julian Groß-Funk
Julian Groß-Funk 2021 年 9 月 15 日
There are so many possible reasons to place the legend in the middle. Here's another one

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

その他の回答 (2 件)

Adam Danz
Adam Danz 2018 年 7 月 24 日
編集済み: Adam Danz 2023 年 2 月 16 日
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;
Update
Starting in MATLAB R2020b (release notes), centering a legend within axes is possible using TiledLayout.
Demo:
tiledlayout(1,2)
nexttile();
fp = fimplicit(@(x,y) y.*sin(x) + x.*cos(y) - 1);
lgd = legend;
lgd.Layout.Tile = 1; % assign tile location
nexttile()
th = linspace(0,2*pi);
x = sin(th).*(5:8)';
y = cos(th).*(5:8)';
plot(x',y')
axis equal
lgd = legend;
lgd.Layout.Tile = 2; % assign tile location
  1 件のコメント
Zhangxi Feng
Zhangxi Feng 2018 年 7 月 24 日
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.

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


laurent jalabert
laurent jalabert 2020 年 12 月 29 日
編集済み: laurent jalabert 2020 年 12 月 29 日
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;
errorbar(x,y,err_y,'-v',...
'Color',markerColour,'MarkerEdgeColor',markerColour,...
'MarkerFaceColor',markerColour,'MarkerSize',10,'LineWidth',2);
hold on; grid on;
end
% 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
h=legend(num2str(STRTe','%.1f'),'Location','NorthWest','NumColumns',3);
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
subplot(sub_col(2),sub_lin(2),total_sub_fig(2));
axis off; legend on;
hlast=legend(num2str(STRTe,'%.1f'),'Location','NorthWest','NumColumns',3);
hposlast = get(hlast,'Position');legend off; % get factive legend position
if nb_subplot(1) < sub_col(1)*sub_lin(1)
set(h,'Position',[hposlast(1),hpos(2),hpos(3),hpos(4)],'FontSize',fs);
else set(h,'Position',[hposlast(1)-0.1,hposlast(2)-0.1,hposlast(3),hposlast(4)],'FontSize',fs);
end
end
end
set(findall(gcf,'-property','FontSize'),'FontSize',fs);
end
end % nb_subplot

カテゴリ

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