How to add legend to the Figure?
70 views (last 30 days)
Show older comments
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 Comments
More 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!