Multicolumn legend

4 visualizzazioni (ultimi 30 giorni)
Julián Francisco
Julián Francisco il 24 Nov 2011
I would like to be able to create a legend to have a number of columns given (for default the legend function has an unique column in Matlab). In the code written below, for example, I need three columns in the legend so this would have two rows lines and three column lines.
X = 0:pi/100:0.25*pi;
Y1 = sin(X);
Y2 = cos(X);
Y3 = tan(X);
Y4 = 0.5;
fh = figure('toolbar','none','menubar','none','Units','characters');
Pan1 = uipanel(fh,'Units','normalized','Position',[0 0 0.5 1],'title',...
'Panel1');
Pan2 = uipanel(fh,'Units','normalized','Position',[0.5 0 0.5 1],'title',...
'Panel2');
haxes = axes('Parent',Pan2,'Units', 'normalized','Position',...
[0.125 0.1 0.75 0.75]);
hplot = plot(haxes,X,Y1,X,Y2,X,Y3,X,Y4);
xlabel(haxes,'Time (second)');
ylabel(haxes,'Amplitude (meter)');
title(haxes,'Trigonometric functions');
Ley = {'Sine function','Cosine function','Tangent function','Constant'};
legend(haxes,Ley,'Location','SouthOutside');
I have tried to use the application called gridlegend on the Matlab File Exchange replacing
legend(haxes,Ley,'Location','SouthOutside');
with
gridLegend(hplot,2,Ley,'Location','SouthOutside');
and
gridLegend(hplot,3,Ley,'Location','SouthOutside');
However, the results obtained are not valid because of the legend's contents appears overlapped and inclusive, for the case 3, is wrong.
P.D. I have tried the columnlegend application in Matlab File Exchange but I need that legend location to be SouthOutside, so it does not work for me.

Risposta accettata

Daniel Shub
Daniel Shub il 26 Nov 2011
MATLAB was not designed to produce publication quality graphs. People have hammered on MATLAB to push its capabilities. If neither columnlegend nor gridlegend can do it, there is likely no easy solution. You need to (1) loosen your constraints (e.g., choose a location other than SouthOutside), (2) be willing to code it your self, (3) create a plot as close as you can and then edit it with a graphics editing program (e.g., Iinkscape or GIMP), or (4) choose a program designed to make nice looking graphs (e.g., Pgfplots).
  3 Commenti
Scott
Scott il 9 Mag 2013
"MATLAB was not designed to produce publication quality graphs."
What quality graphs was it designed to produce? Because it's certainly designed to produce graphs.
Daniel Shub
Daniel Shub il 13 Mag 2013
@Scott there is a huge difference between software that is designed to produce graphs and one designed to produce publication quality graphs. Think about the difference between MS Word and MS Publisher, both are designed to produce text based documents, but only one is designed to produce publication quality text documents.

Accedi per commentare.

Più risposte (1)

Julián Francisco
Julián Francisco il 4 Dic 2011
Here is my solution for a legend which has a 'SouthOutside' location (I have supposed that the axes graphic object has 'normalized' units and its 'Position' is [0.1 0.1 0.8 0.8]):
function CLegend(hax,numcol,Ley)
%# Inputs
% hax : handle of the axes object to which belongs the legend
% numcol: number of columns for the legend
% Ley: text strings (labels) for the legend
set(hax,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
set(hax,'Units','characters');
posAx = get(hax,'Position');
insAx = get(hax,'TightInset');
[legend_h,object_h] = legend(hax,Ley,'Units','characters','Location',...
'South','Orientation','vertical');
posl = get(legend_h,'Position');
numlines = length(Ley);
if (numlines<numcol)
numcol = numlines;
end
numpercolumn = ceil(numlines/numcol);
if (mod(numlines,numpercolumn) == 0)
numcol = numlines/numpercolumn;
end
l = zeros(1,numlines);
a = zeros(1,numlines);
h = zeros(1,4);
for j=1:numlines
h = get(object_h(j),'Extent');
l(j) = h(3);
a(j) = h(4);
set(object_h(j),'Units','characters');
end
lmax = posl(3)*max(l);
hmax = posl(4)*max(a);
hLine = object_h(numlines+1);
xdata = get(hLine, 'xdata');
dx = xdata(2)-xdata(1);
di = 2;
sheight = hmax;
height = hmax*numpercolumn-sheight/2;
line_width = dx*posl(3);
spacer = xdata(1)*posl(3);
delta1 = spacer + line_width + spacer + lmax;
delta2 = line_width + spacer + lmax + spacer;
delta3 = lmax + spacer + line_width + spacer;
factx = 1/(posl(3)*numcol);
facty = 1/(hmax*numpercolumn);
width_l = numcol*delta1;
set(legend_h, 'Position', [posAx(1) + 0.5*(posAx(3)-width_l) posl(2) ...
width_l numpercolumn*hmax]);
col_ind = -1;
row_ind = -1;
j = 0;
for i=1:numlines,
if strcmpi(orient,'horizontal'),
if mod(i,numcol)==1,
row_ind = row_ind+1;
end
col_ind = mod(i,numcol)-1;
if col_ind == -1,
col_ind = numcol-1;
end
else
if numpercolumn==1 || mod(i,numpercolumn)==1,
col_ind = col_ind+1;
end
row_ind = mod(i,numpercolumn)-1;
if row_ind == -1,
row_ind = numpercolumn-1;
end
end
if (i==1)
linenum = i+numlines;
else
linenum = linenum+di;
end
labelnum = i;
set(object_h(linenum), 'ydata',facty*[height-row_ind*sheight ...
height-row_ind*sheight]);
set(object_h(linenum), 'xdata', factx*[spacer + j*delta2 ...
spacer + j*delta2 + line_width]);
set(object_h(linenum+1), 'ydata',facty*(height-row_ind*sheight));
set(object_h(linenum+1), 'xdata', factx*(spacer+line_width/2));
set(object_h(labelnum), 'Position', [j*delta3+spacer*2+line_width ...
height-row_ind*sheight]);
if (mod(i,numpercolumn)== 0)
j = j + 1;
end
end
opl = get(legend_h,'OuterPosition');
set(hax, 'Position',[posAx(1) posAx(2)+opl(4) posAx(3) posAx(4)-opl(4)]);
set(legend_h, 'OuterPosition',[opl(1) (posAx(2)-insAx(2))/2 opl(3) opl(4)]);
set(hax,'Units','normalized');
end

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by