Effacer les filtres
Effacer les filtres

How to add legend to the Figure?

92 vues (au cours des 30 derniers jours)
Atanu
Atanu le 2 Déc 2022
Modifié(e) : Atanu le 3 Déc 2022
I have a plot for which I am trying add legends. I am trying to add the shaded rectangles as well in legend. Please note that the rectangles are object. But they are not appearing in the legend.
I have also added the part of my code for reference. How can I add legend for the rectangle with a small same color rectagle as symbol?
% plot data
h = figure;
plot(xWithTone,yWithTone,'Color',[0.9 0.7 0.1],'LineWidth',1);
hold on;
plot(xNormalized,yNormalized,'b','LineWidth',1);
plot(xNormalized(1),yNormalized(1),'go','MarkerSize',10,'LineWidth',2);
plot(xNormalized(end),yNormalized(end),'ro','MarkerSize',10,'LineWidth',2);
% set figure limit
maze = {'maze2','maze1','maze3','maze4'};
figureLimit = {{[-0.2 1.2],[-0.2 1.2]},{[-1.2 0.2],[-0.2 1.2]}, ...
{[-1.2 0.2],[-1.2 0.2]},{[-0.2 1.2],[-1.2 0.2]}};
% get the index in maze array
mazeIndex = find(ismember(maze,subject_data.mazenumber));
xlim(figureLimit{mazeIndex}{1}); ylim(figureLimit{mazeIndex}{2});
% create rectangles
switch mazeIndex
case 1
% Maze2 a.k.a 1st quadrant
% rectangles are denoted according to feeder numbers in this maze
r1 = rectangle('Position',[0.8 0 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r2 = rectangle('Position',[0 0 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r3 = rectangle('Position',[0 0.8 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r4 = rectangle('Position',[0.8 0.8 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r = [r1,r2,r3,r4];
set(r(subject_data.feeder),'FaceColor', [1 1 0 0.3]);
rectangle('Position',[0.25 0.25 0.5 0.5],'EdgeColor','none', ...
'FaceColor',[1 0 0 0.2]);
case 2
% Maze1 a.k.a 2nd quadrant
% rectangles are denoted according to feeder numbers in this maze
r1 = rectangle('Position',[-1 0.8 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r2 = rectangle('Position',[-1 0 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r3 = rectangle('Position',[-0.2 0 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r4 = rectangle('Position',[-0.2 0.8 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r = [r1,r2,r3,r4];
set(r(subject_data.feeder),'FaceColor', [1 1 0 0.3]);
rectangle('Position',[-0.75 0.25 0.5 0.5],'EdgeColor','none', ...
'FaceColor',[1 0 0 0.2]);
case 3
% Maze3 a.k.a 3rd quadrant
% rectangles are denoted according to feeder numbers in this maze
r1 = rectangle('Position',[-0.2 -1 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r2 = rectangle('Position',[-0.2 -0.2 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r3 = rectangle('Position',[-1 -0.2 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r4 = rectangle('Position',[-1 -1 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r = [r1,r2,r3,r4];
set(r(subject_data.feeder),'FaceColor', [1 1 0 0.3]);
rectangle('Position',[-0.75 -0.75 0.5 0.5],'EdgeColor','none', ...
'FaceColor',[1 0 0 0.2]);
legend({'feeders','offer','central zone'},'Location','northeastoutside');
case 4
% Maze4 a.k.a 4th quadrant
% rectangles are denoted according to feeder numbers in this maze
r1 = rectangle('Position',[0 -0.2 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r2 = rectangle('Position',[0.8 -0.2 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r3 = rectangle('Position',[0.8 -1 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r4 = rectangle('Position',[0 -1 0.2 0.2],'EdgeColor','none', ...
'FaceColor',[0.3 0.3 0.3 0.3]);
r = [r1,r2,r3,r4];
set(r(subject_data.feeder),'FaceColor', [1 1 0 0.3]);
rectangle('Position',[0.25 -0.75 0.5 0.5],'EdgeColor','none', ...
'FaceColor',[1 0 0 0.2]);
otherwise
warning('Unexpected maze number.')
end
% add legend
legend({'before trial tone','after trial tone','location at tone', ...
'final location','feeders','offer','central zone'}, ...
'Location','northeastoutside');
  3 commentaires
Atanu
Atanu le 2 Déc 2022
Thank you! Let me check it out.
Atanu
Atanu le 3 Déc 2022
Done, thank you for your help!

Connectez-vous pour commenter.

Réponse acceptée

Atanu
Atanu le 3 Déc 2022
Modifié(e) : Atanu le 3 Déc 2022
I made patches of same color as rectangle to add legends for them. Also added handle to plot functions to make it uniform with patch objects while adding legends.
% plot normalized data
h = figure;
p1 = plot(xWithTone,yWithTone,'Color',[0.9 0.7 0.1],'LineWidth',1);
hold on;
p2 = plot(xNormalized,yNormalized,'b','LineWidth',1);
mrkr1 = plot(xNormalized(1),yNormalized(1),'go','MarkerSize',10,'LineWidth',2);
mrkr1 = plot(xNormalized(end),yNormalized(end),'ro','MarkerSize',10,'LineWidth',2);
%
% some more code
%
% gray patch
grayPatch = patch(nan,nan,'k');grayPatch.FaceColor = [0.3 0.3 0.3];
grayPatch.FaceAlpha = 0.3;grayPatch.EdgeColor = "none";
% yellow patch
yellowPatch = patch(nan,nan,'k');yellowPatch.FaceColor = [1 1 0];
yellowPatch.FaceAlpha = 0.3;yellowPatch.EdgeColor = "none";
% red patch
redPatch = patch(nan,nan,'k');redPatch.FaceColor = [1 0 0];
redPatch.FaceAlpha = 0.2;redPatch.EdgeColor = "none";
% add legend
legend([p1,p2,mrkr1,mrkr2,grayPatch,yellowPatch,redPatch], ...
{'before trial tone','after trial tone','location at tone','final location', ...
'feeders','offer','central zone'},'Location','northeastoutside');

Plus de réponses (0)

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by