Azzera filtri
Azzera filtri

Place legend under all subplots and proper resize figure window

20 visualizzazioni (ultimi 30 giorni)
I tried to place one legend under my eight subplots (4x2). I tried to manipulate the position of the legend by defining a manual position vector. Actually this works BUT if I use a larger font size my legend clips out of the visible window of the figure. There is not enough space under the bottom xlable - so simply re-positioning the legend is not working.
However, it is possible to get around this by simply adding fake subplots under the eight real subplots. This leads to another problem that the eight suplots gets distorted in the y-direction which I can't fix, because it is not possible to make the figure window larger then the monitor resolution.
Next trick I tried is the print function. It doesn't work properly when I try to export the figure as an .emf or any other vector file format, because the subplot titles and the legend gets weirdly distorted.
Last thing I tried is export_fig from FileExchange. However, it doen't seem that I can define a aspect ratio nor a resolution. Also .emf is not supported.
Does anyone know how fix my problem?

Risposte (1)

Akshay Khadse
Akshay Khadse il 4 Set 2018
To get a common legend for all the subplots, you need to create a subplot spanning all the columns of the figure, hide its axes and use the 'legend' command to specify where to put the legend and which data the legend is meant to address. A short example for this is below:
% Create figure
figure('Position',[1000 540 674 798]);
% Create subplots and save handles
subplot(5,2,1); h1 = plot(rand(1,10),'r');
subplot(5,2,2); h2 = plot(rand(1,10),'g');
subplot(5,2,3); h3 = plot(rand(1,10),'b');
subplot(5,2,4); h4 = plot(rand(1,10),'k');
subplot(5,2,5); h5 = plot(rand(1,10),'m');
subplot(5,2,6); h6 = plot(rand(1,10),'c');
subplot(5,2,7); h7 = plot(rand(1,10),'y');
subplot(5,2,8); h8 = plot(rand(1,10),'r-*');
% Create dummy subplot for legend
hLegend = subplot(5,2,9.5);
posLegend = get(hLegend,'Position');
% Creating the legend
leg = legend(hLegend,[h1;h2;h3;h4;h5;h6;h7;h8],'Plot1','Plot2','Plot3','Plot4','Plot5','Plot6','Plot7','Plot8');
axis(hLegend,'off');
set(leg,'Position',posLegend);
However, with this approach, distinct markers and colors have to specified explicitly in each plot as otherwise all the plots would be of same style.

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by