Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Seperate Legend from Figure

1,540 views
Skip to first unread message

Mario Liverpool

unread,
Aug 8, 2011, 8:12:10 AM8/8/11
to
Hi,

I am plotting a series of figures which all have the same legend.
I don't want to repeat the same legend for every figure. I would like
to plot all the figures seperately and then put ONE legend for them all
under the main caption.
I am after not after capturing the legend (with CorelDraw)
and cropping it then including it as a seperate file.
I am after a way to produce a figure (with Matlab or EPS file) which contains
only the legend without any accompanying plot?
Any help please.
Thanks,
Mario

Kelly Kearney

unread,
Aug 8, 2011, 11:57:30 AM8/8/11
to
"Mario Liverpool" wrote in message <j1ojqq$98l$1...@newscl01ah.mathworks.com>...

Perhaps legendflex (http://www.mathworks.com/matlabcentral/fileexchange/31092-legendflex-a-more-flexible-legend) could help? The following example creates a figure with some lines plotted, then creates a legend for that plot on a separate figure. Save the figure using your print-to-file function of choice.

fig1 = figure;
hl = plot(1:10, rand(2,10))
fig2 = figure;
legendflex(hl, {'Thing 1', 'Thing 2'}, 'ref', fig2)

someone

unread,
Aug 8, 2011, 12:03:14 PM8/8/11
to
"Kelly Kearney" wrote in message <j1p11a$rg9$1...@newscl01ah.mathworks.com>...

Or you might give this a try:

http://www.mathworks.com/matlabcentral/fileexchange/29191-sub-plot-legend

OJ

unread,
Jan 25, 2013, 5:21:50 PM1/25/13
to
what just helped me was this:
"Just go to view and then to plot browser. Click on axis and data and hide them, the legend and the color bar will be the only things visible. You can then save that figure as Matlab figure o print it." quote from:http://www.quora.com/MATLAB/How-can-I-export-or-print-only-the-legend-or-the-colorbar-from-an-axis
You can also do it by code commands like this:

plot(1:10, rand(2,10))
fn = 'plot';%filename
l = 1:length(fn);
fn = sprintf('%s_LEGEND',fn(l));%give it a separate filename

a = get(gcf,'children');% link to ledgend is now a(1)
b = get(gca,'children');% link to the data curve/s

set(a,'visible','off'); %hide axes etc...
set(b,'visible','off'); %hide data...

legfs = get(a(1),'Fontsize'); %get legend fontsize
set(a(1),'Fontsize',legfs+1); %make legend appear larger

%... print it...

%reset everything to notmal plot:
set(a,'visible','on');
set(b,'visible','on');
fn = fn(l);
set(a(1),'visible','off'); %hide only legend from plot

%... print again...

hope that gives you an idea.
cheers

lfst...@vt.edu

unread,
Jan 3, 2019, 4:57:48 PM1/3/19
to
This is an old question I know, but I just spend some time figuring out how to do this using Kelly's idea but without "legendflex".

The following code produces a data figure and a separate legend of outer dimension 4 x 2inches and centered inner dimensions of 3.5 x 1.5 inches.

Giving ax2 a width of 0 inches and a height of 0 inches, "erases" the figure 2 coordinate plane.

---------------------------------------------------------------------------
fig1 = figure;
hl = plot(1:10, rand(2,10));

fig2 = figure;
set(fig2,'Units','inches')
set(fig2,'Position',[5 5 4 2]);
%Note: Figure Position = [left bottom width height] where left and bottom] are measured from the lower left corner of the monitor.

ax2 = gca;
set(ax2,'Units','inches')
set(ax2,'Position',[0 0 0 0]);
%Note: Axes Position = [left bottom width height] where left and bottom] are measured from the lower left corner of the figure.

legend(ax2, hl, {'Thing 1', 'Thing 2'},'Position',[.25/4 .25/2 3.5/4 1.5/2])
%Note: Legend Position = [left/figurewidth bottom/figureheight width/figurewidth height/figureheight],i.e. Legend positions must be specified relative to the figure dimensions if you are not using "legendflex".
0 new messages