Adding same legend to multiple plots in a for loop
2 views (last 30 days)
Show older comments
I want to introduce a legend in a for loop while inside the for loop I have two plots and the legend should be same for both. I tried some answers but I don't know why it's not working. Maybe I'm doing something else wrong. Here is the code btw
%%Create figures
figure1 = figure;
figure2 = figure;
axes1 = axes('Parent',figure1);
hold(axes1,'on');
axes2 = axes('Parent',figure2);
hold(axes2,'on');
Legend1 = cell(1,3);
Legend2 = cell(1,3);
hold all
%%Loop to get multiple outputs for multiple ranks
for i = 1:max(size(Rank))
[X{i},Y{i}] = some_function(x1,x2,x3,bla);
% Create plot for X
fig1=plot(X{i},'Parent',axes1,'LineWidth',2);
title('Plot for X');
xlabel({'X label'});
ylabel({'Y label'});
Legend1{i} = strcat('Pass', num2str((i)));
legend(Legend1{i})
[~,objh,~,~] = legend(Legend1{i});
set(objh,'linewidth',2);
% Create plot for Y
fig2=plot(Y{i},'Parent',axes2,'LineWidth',2);
title('Plot for Y');
xlabel({'X label'});
ylabel({'Y label'});
Legend2{i} = strcat('Rank', num2str((i)));
legend(Legend2{i})
[~,objh,~,~] = legend(Legend2{i});
set(objh,'linewidth',2);
end
%%Plot stuff
legend1 = legend(axes1,'show');
legend2 = legend(axes2,'show');
box(axes1,'on');
box(axes2,'on');
set(legend1,...
'Position',[0.740476192604927 0.655158736970689 0.160714283106583 0.24404761223566]);
set(legend2,...
'Position',[0.740476192604927 0.655158736970689 0.160714283106583 0.24404761223566]);
1 Comment
Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!