How to add a single legent with multiple subplots with out overlapping on plots?

3 ビュー (過去 30 日間)
Hello All,
I am trying to add a add a common legend for all my subplots in a graph. But I dont have extra space for the legend to add on the plots. They are overlapping on one another. I need something like in the image. Can some one please hlp me with it. My code is as follows:
Thanks in advance.
Z = readtable('Excel.xlsx') ;
data = table2array(Z) ;
subplot(2,3,1)
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
annotation('textbox', [0.25, 0.77, 0.1, 0.1], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
subplot(2,3,2)
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
annotation('textbox', [0.53, 0.77, 0.1, 0.1], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
subplot(2,3,3)
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
annotation('textbox', [0.811, 0.77, 0.1, 0.1], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
title('Tangential velocity fluctuations')
subplot(2,3,4)
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
annotation('textbox', [0.25, 0.32, 0.1, 0.1], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
subplot(2,3,5)
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
annotation('textbox', [0.53, 0.32, 0.1, 0.1], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
subplot(2,3,6)
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
annotation('textbox', [0.811, 0.32, 0.1, 0.1], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
fig = gcf;
fig.Position(3) = fig.Position(3) + 250;
legend('Plot1', 'Plot2', 'Plot3', 'Plot4', 'Location', 'bestoutside', 'FontSize',10);
Lgnd = legend('show');
lgd.FontSize = 18;
Lgnd.Position(1) = 0.8;
Lgnd.Position(2) = 0.4;
sgtitle(["titel"])
x0=12;
y0=2;
width=1200;
height=760;
set(gcf,'position',[x0,y0,width,height])
set(gcf,'PaperOrientation','landscape');
  3 件のコメント
Vishnuvardhan Naidu Tanga
Vishnuvardhan Naidu Tanga 2022 年 10 月 19 日
Thank you for the reply. But is it possible to have it completely outside of the plots, not in between.
Jan Böttner
Jan Böttner 2023 年 8 月 15 日
移動済み: Voss 2023 年 8 月 15 日
Hi!
I am having a similar issue, only that I wnat my Legend below the tiles.
Unfortunately it covers a part of the xlabel. Hast anyone an Idea on how to fix this?
figure('Position',[1 1 1000 400])
COtwo = tiledlayout(1,3);
nexttile
%-------CO2-emissions--today----------------------------
h_ss = bar(out.emissions.today{:,1},out.emissions.today{:,[10 12]},'stacked','BarWidth',1/3.5,'XData',out.emissions.today{:,1}-1/3.5,'FaceAlpha',0.5,'EdgeColor','none');
h_ss(1).FaceColor = [0 0.4470 0.7410];
h_ss(2).FaceColor = [0.26 0.75 0.98];
hold on
h_us = bar(out.emissions.today{:,1},out.emissions.today{:,[7 8]},'stacked','BarWidth',1/3.5,'XData',out.emissions.today{:,1},'FaceAlpha',0.5,'EdgeColor','none');
h_us(1).FaceColor = [0.4660 0.6740 0.1880] ; %[0.26 0.44 0.26];
h_us(2).FaceColor = [0.6660 0.8740 0.4880];
hold on
h_us = bar(out.emissions.today{:,1},out.emissions.today{:,[17 11]},'stacked','BarWidth',1/3.5,'XData',out.emissions.today{:,1}+1/3.5,'FaceAlpha',0.5,'EdgeColor','none');
h_us(1).FaceColor = [0.7 0.7 0.7];
h_us(2).FaceColor = [0.83 0.83 0.83];
hold off
%legend(out.emissions.today.Properties.VariableNames([10,12,7,8,17,11]), ...
% 'Location','SouthOutside','NumColumns',1,'Orientation','Horizontal')
ylabel('CO2-Equivalents');
xlabel('Bivalence Temperature');
xscale = Tmin:2:Tmax;
xticks(Tmin-2:2:Tmax+2);
xticklabels({'HP',xscale,'LGB'})
axis([out.emissions.today{1,1}-1,out.emissions.today{end,1}+1,0,3.5])
txt = {'CO2-Emission factors:',['Biomass = ' num2str(Basisdaten.Emissionsfaktor.Biomasse.today) ' kg/kWh'], ['Electricity = ' num2str(Basisdaten.Emissionsfaktor.Strom.today) ' kg/kWh'],};
dim = [0.228 0.95 0 0]; %[.365 .87 0 0];
annotation('textbox',dim,'String',txt,'FitBoxToText','on');
%-------CO2-emissions--midterm----------------------------
nexttile
h_ss = bar(out.emissions.midterm{:,1},out.emissions.midterm{:,[10 12]},'stacked','BarWidth',1/3.5,'XData',out.emissions.midterm{:,1}-1/3.5,'FaceAlpha',0.5,'EdgeColor','none');
h_ss(1).FaceColor = [0 0.4470 0.7410];
h_ss(2).FaceColor = [0.26 0.75 0.98];
hold on
h_us = bar(out.emissions.midterm{:,1},out.emissions.midterm{:,[7 8]},'stacked','BarWidth',1/3.5,'XData',out.emissions.midterm{:,1},'FaceAlpha',0.5,'EdgeColor','none');
h_us(1).FaceColor = [0.4660 0.6740 0.1880] ; %[0.26 0.44 0.26];
h_us(2).FaceColor = [0.6660 0.8740 0.4880];
hold on
h_us = bar(out.emissions.midterm{:,1},out.emissions.midterm{:,[17 11]},'stacked','BarWidth',1/3.5,'XData',out.emissions.midterm{:,1}+1/3.5,'FaceAlpha',0.5,'EdgeColor','none');
h_us(1).FaceColor = [0.7 0.7 0.7];
h_us(2).FaceColor = [0.83 0.83 0.83];
legend(out.emissions.midterm.Properties.VariableNames([10,12,7,8,17,11]), ...
'Location','southoutside','NumColumns',6,'Orientation','Horizontal')
ylabel('CO2-Equivalents');
xlabel('Bivalence Temperature');
xscale = Tmin:2:Tmax;
xticks(Tmin-2:2:Tmax+2);
xticklabels({'HP',xscale,'LGB'})
axis([out.emissions.midterm{1,1}-1,out.emissions.midterm{end,1}+1,0,3.5])
txt = {'CO2-Emission factors:',['Biomass = ' num2str(Basisdaten.Emissionsfaktor.Biomasse.midterm) ' kg/kWh'], ['Electricity = ' num2str(Basisdaten.Emissionsfaktor.Strom.midterm) ' kg/kWh'],};
dim = [0.545 0.95 0 0]; %[.365 .87 0 0];
annotation('textbox',dim,'String',txt,'FitBoxToText','on');
%-------CO2-emissions--longterm----------------------------
nexttile
h_ss = bar(out.emissions.longterm{:,1},out.emissions.longterm{:,[10 12]},'stacked','BarWidth',1/3.5,'XData',out.emissions.longterm{:,1}-1/3.5,'FaceAlpha',0.5,'EdgeColor','none');
h_ss(1).FaceColor = [0 0.4470 0.7410];
h_ss(2).FaceColor = [0.26 0.75 0.98];
hold on
h_us = bar(out.emissions.longterm{:,1},out.emissions.longterm{:,[7 8]},'stacked','BarWidth',1/3.5,'XData',out.emissions.longterm{:,1},'FaceAlpha',0.5,'EdgeColor','none');
h_us(1).FaceColor = [0.4660 0.6740 0.1880] ; %[0.26 0.44 0.26];
h_us(2).FaceColor = [0.6660 0.8740 0.4880];
hold on
h_us = bar(out.emissions.longterm{:,1},out.emissions.longterm{:,[17 11]},'stacked','BarWidth',1/3.5,'XData',out.emissions.longterm{:,1}+1/3.5,'FaceAlpha',0.5,'EdgeColor','none');
h_us(1).FaceColor = [0.7 0.7 0.7];
h_us(2).FaceColor = [0.83 0.83 0.83];
%legend(out.emissions.longterm.Properties.VariableNames([10,12,7,8,17,11]), ...
% 'Location','SouthOutside','NumColumns',1,'Orientation','Horizontal')
ylabel('CO2-Equivalents');
xlabel('Bivalence Temperature');
xscale = Tmin:2:Tmax;
xticks(Tmin-2:2:Tmax+2);
xticklabels({'HP',xscale,'LGB'})
axis([out.emissions.longterm{1,1}-1,out.emissions.longterm{end,1}+1,0,3.5])
txt = {'CO2-Emission factors:',['Biomass = ' num2str(Basisdaten.Emissionsfaktor.Biomasse.longterm) ' kg/kWh'], ['Electricity = ' num2str(Basisdaten.Emissionsfaktor.Strom.longterm) ' kg/kWh'],};
dim = [0.865 0.95 0 0];
annotation('textbox',dim,'String',txt,'FitBoxToText','on');
%-----plot-properties-------------------------------------------
COtwo.TileSpacing = 'compact';
COtwo.Padding = 'compact';
set(gcf,'WindowState','maximized');

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

採用された回答

Star Strider
Star Strider 2022 年 10 月 19 日
An alternative —
Z = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1161928/Excel.xlsx', 'VariableNamingRule','preserve') ;
data = table2array(Z) ;
subplot(2,4,1)
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
% hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
% hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
pos = get(gca,'Position'); % ADDED
annotation('textbox', [pos(1)+0.1, 0.77, 0.1, 0.1], 'String', "X", 'FontSize',12)
% annotation('textbox', [0.25, 0.77, 0.1, 0.1], 'String', "X", 'FontSize',12) % CHANGED
grid on
xlabel('x');
ylabel('y');
subplot(2,4,2)
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
pos = get(gca,'Position'); % ADDED
annotation('textbox', [pos(1)+0.1, 0.77, 0.1, 0.1]+[0.01 0 0 0], 'String', "X", 'FontSize',12)
% annotation('textbox', [0.53, 0.77, 0.1, 0.1]+[0.01 0 0 0], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
subplot(2,4,3)
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
pos = get(gca,'Position'); % ADDED
annotation('textbox', [pos(1)+0.1, 0.77, 0.1, 0.1]+[0.81*0.01 0 0 0], 'String', "X", 'FontSize',12)
% annotation('textbox', [0.811, 0.77, 0.1, 0.1]+[0.81*0.01 0 0 0], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
title('Tangential velocity fluctuations')
subplot(2,4,5)
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
pos = get(gca,'Position'); % ADDED
annotation('textbox', [pos(1)+0.1, 0.32, 0.1, 0.1]+[0.25*0.01 0 0 0], 'String', "X", 'FontSize',12)
% annotation('textbox', [0.25, 0.32, 0.1, 0.1]+[0.25*0.01 0 0 0], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
subplot(2,4,6)
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
pos = get(gca,'Position'); % ADDED
annotation('textbox', [pos(1)+0.1, 0.32, 0.1, 0.1]+[0.53*0.01 0 0 0], 'String', "X", 'FontSize',12)
% annotation('textbox', [0.53, 0.32, 0.1, 0.1]+[0.53*0.01 0 0 0], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
subplot(2,4,7)
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
pos = get(gca,'Position'); % ADDED
annotation('textbox', [pos(1)+0.1, 0.32, 0.1, 0.1]+[0.81*0.01 0 0 0], 'String', "X", 'FontSize',12)
% annotation('textbox', [0.811, 0.32, 0.1, 0.1]+[0.81*0.01 0 0 0], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
fig = gcf;
fig.Position(3) = fig.Position(3) + 250;
legend('Plot1', 'Plot2', 'Plot3', 'Plot4', 'Location', 'bestoutside', 'FontSize',10);
Lgnd = legend('show');
lgd.FontSize = 18;
Lgnd.Position(1) = 0.8;
Lgnd.Position(2) = 0.4;
hsp2448 = subplot(2,4,[4 8]);
% get(hsp2448)
hsp2448.Visible = 'off';
Lgnd.Position = hsp2448.Position+[0 +0.25 0 -0.55]; % Change Last 3 Values As Required
sgtitle(["titel"])
x0=12;
y0=2;
width=1200;
height=760;
set(gcf,'position',[x0,y0,width,height])
set(gcf,'PaperOrientation','landscape');
.
  6 件のコメント
Kevin Holly
Kevin Holly 2022 年 10 月 20 日
Looks like my answer got deleted, so reposting here. I used nexttile instead of subplot.
Z = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1161928/Excel.xlsx', 'VariableNamingRule','preserve') ;
data = table2array(Z);
tiledlayout(2,3)
nexttile
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
annotation('textbox', [0.25, 0.77, 0.1, 0.1], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
nexttile
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
annotation('textbox', [0.53, 0.77, 0.1, 0.1], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
nexttile
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
annotation('textbox', [0.811, 0.77, 0.1, 0.1], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
title('Tangential velocity fluctuations')
nexttile
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
annotation('textbox', [0.25, 0.32, 0.1, 0.1], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
nexttile
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
annotation('textbox', [0.53, 0.32, 0.1, 0.1], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
nexttile
plot(data(:,1), data(:,2), 'ks', 'linewidth', 1);
hold on
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
plot(data(:,5), data(:,6), 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold off
annotation('textbox', [0.811, 0.32, 0.1, 0.1], 'String', "X", 'FontSize',12)
grid on
xlabel('x');
ylabel('y');
fig = gcf;
fig.Position(3) = fig.Position(3) + 250;
legend('Plot1', 'Plot2', 'Plot3', 'Plot4', 'Location', 'bestoutside', 'FontSize',10);
Lgnd = legend('show');
Lgnd.FontSize = 18;
Lgnd.Position(1) = 0.8;
Lgnd.Position(2) = 0.4;
sgtitle(["titel"])
x0=12;
y0=2;
width=1200;
height=760;
set(gcf,'position',[x0,y0,width,height])
Lgnd.Layout.Tile = 'east';
Star Strider
Star Strider 2022 年 10 月 20 日
I noticed it was missing.
I have no idea how it got deleted, and just assumed you deleted it. Rena Berman might have some insight into that.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFilter Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by