- Colors: Pick a set of distinct colors. You can use something like lines(5) in MATLAB to get five different colors.
- Looping: For each instance, loop through its variations. Plot each variation using the color for that instance.
- Store Handles: Save the plot handle for the first variation of each instance. This handle will be used for the legend.
- Create Legend: After plotting, use the stored handles to make a legend. This way, your legend only shows one entry per instance, not every single variation.
Group Data for one Legend in Figure of Multiple SubPlots
39 views (last 30 days)
Show older comments
Hi All,
I am working with several instances that I want to plot data for each with their own data set. For example I have four plots with their own respective signals I need to look at and for multiple instances. Each instance is comprised of several variations that I need to be of the same color. In each plot each instance is colored differently but each data set of variations is one color based on instance. I have been scouring the internet for help on this but I can't seem to get any solution to this issue.
What i would like to do is have one legend (and yes I know i could just add a legend to one of my subplots and move it) that only has the different instances and their respective colors without adding in all of the variations within the data set. I have a loop set up to plot 5 variations per instance and I was hoping to loop this through instances that use different files. Below is kind of the code I've been working around and trying to make work. Its just a section for ease, but basically lets say I have 5 instances and each has 5 variations, I can plot them and ensure each variation is the same color so i have 5 different colors on the plots, but I want a legent just to show the 5 instances in their respective colors and not 25 for evry line.
Please help me out here, I am starting to go nuts and found some very terrible ways of diong this by hand which aren't applicable at all.
for z = 1:num files;
subplot(1,2,2);
hold on
grid on;
for i = 1:5
plot(z) = plot(StartA(:,i),AMA_1(:,i),color)
end
end
legend(plot(1),plot(2), etc...
3 Comments
Adam Danz
on 31 Jan 2025
prabhat kumar sharma's solution is better than the suggestion to setting HandleVisibility. It has more control of what goes into the legend.
Accepted Answer
Adam Danz
on 31 Jan 2025
Edited: Adam Danz
on 31 Jan 2025
Here are the key concepts to achieve this
- Use tiledlayout instead of subplot. Tiledlayout does a much better job at hosting global legends.
- Use nexttile to generate the axes within the tiled layout
- Set the DisplayName property on the graphics objects to specify the name used in the legend
- Store the graphics object handles (for the next step)
- Specify the object handles in your call to legend. This increases control of what goes in the legend.
It looks like you are plotting one line at a time so I'll use that patterns in this demo.
% Define line colors and the legend name for each color
colors = turbo(4);
names = ["A" "B" "C" "D"];
fig = figure();
tcl = tiledlayout(2,3); % 2 x 3 layout
h = gobjects(6,4); % Store handles in this variable
for j = 1:6 % loop through axes
ax = nexttile(tcl);
hold(ax,'on')
for i = 1:4 % loop through individual lines
h(j,i) = plot(ax, 1:10, sort(randg(3,1,10)), ... % store handle
'color', colors(i,:), ... % assign color
'DisplayName', names(i)); % assign legend name
end
end
% Now we just have to tell legend which handles to include.
leg = legend(h(1,:));
% Put the legend in the right side margin
leg.Layout.Tile = 'east';
0 Comments
More Answers (1)
Tony
on 31 Jan 2025
('HandleVisibility', 'off') prevents the plot from adding an entry to the legend. So you can set this 'on' (default value) for only the first instance of the variation, and 'off' for all other instances
n_inst = 5;
n_var = 5;
n_length = 10;
data = rand(n_length, n_var, n_inst);
color_set = lines(n_var);
figure(); hold on; grid on;
for i = 1:n_inst
for j = 1:n_var
h = plot(data(:, j, i), 'Color', color_set(j,:), 'LineWidth', 2);
if i > 1
h.HandleVisibility = 'off';
end
end
end
legend
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!