Adding Legend to Subplot

20 ビュー (過去 30 日間)
Ollie Bardsley
Ollie Bardsley 2020 年 12 月 1 日
回答済み: VBBV 2024 年 7 月 21 日
Hiya,
I've set up the following code to load in a series of .fig files I'm using to create a multipanel figure. I want to generate a legend for underneath each of the 2 columns of this 6x2 figure. Each of the .fig files plots and opens with a legend, but this is lost when I'm putting it into the Subplot. This function is HORRIBLE and I've spent all day trying to do this. Please help!
LF = ["0 1.fig","0 2.fig","10 1.fig","10 2.fig","20 1.fig","20 2.fig","30 1.fig","30 2.fig","40 1.fig","40 2.fig","45 1.fig","45 2.fig"];
%Define new figure
figure1 = figure;
hold on
for i = 1:12
x = subplot(6,2,i,'parent',figure1);
f = hgload(LF(i));
copyobj(allchild(get(f,'CurrentAxes')),x);
x.YLim = [0,25];
x.YTick = [0:5:25];
if i/2 == int32(i/2)
x.XLim = [0 110];
x.XTick = [0:10:110];
else
x.XLim = [0 5e-4];
x.XTick = [0:1e-4:5e-4];
end
end
  1 件のコメント
amin
amin 2020 年 12 月 1 日
Hi Ollie,
The question is not clear for me.
There is no command to generate legend in your code.
Also, the legend adds text inside a figure, not underneath nor top. You can use 'title' to add a title on top of your figure.
'hgload(LF(i))' loads the figure LF(i) and displays it, if you do not want to have it displayed, you can replace it with:
f = openfig(LF(i),'invisible');
If you explain more, maybe I can help.

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

採用された回答

VBBV
VBBV 2024 年 7 月 21 日
@Ollie Bardsley, you need to rearrange the few lines of code inside the for loop as shown below
LF = ["0 1.fig","0 2.fig","10 1.fig","10 2.fig","20 1.fig","20 2.fig","30 1.fig","30 2.fig","40 1.fig","40 2.fig","45 1.fig","45 2.fig"];
% new figure
figure1 = figure;
hold on
%///////Rearrange the following lines after the for loop
for i = 1:12
f = hgload(LF(i)); % first access /load the saved figure using hgload function
ax_new = copyobj(allchild(get(f,'CurrentAxes')),figure1); % copy all the object handles array from saved figure to new figure
x = subplot(6,2,i,'parent',ax_new{1}); % use the desired axes handles to the subplot (cell array)
%//// this code remains same
x.YLim = [0,25];
x.YTick = [0:5:25];
if i/2 == int32(i/2)
x.XLim = [0 110];
x.XTick = [0:10:110];
else
x.XLim = [0 5e-4];
x.XTick = [0:1e-4:5e-4];
end
end

その他の回答 (1 件)

Divija Aleti
Divija Aleti 2021 年 1 月 21 日
Hi Ollie,
I understand that you want to be able to copy the legends of the figures, along with their plots, into the subplot but are unable to do so.
I have brought this issue to the notice of our developers. They will investigate the matter further.
Regards,
Divija

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by