Group Data for one Legend in Figure of Multiple SubPlots
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
prabhat kumar sharma
on 31 Jan 2025
When you're dealing with multiple instances and variations, each with its own color scheme, and you want a clean legend that only shows the instances, here's a simple approach:
- 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.
colors = lines(5); % Five distinct colors
plotHandles = gobjects(1, 5); % Preallocate for legend handles
for instance = 1:5
for variation = 1:5
% Assume StartA and AMA_1 contain your data
h = plot(StartA(:, variation), AMA_1(:, variation), 'Color', colors(instance, :));
if variation == 1
plotHandles(instance) = h; % Save the handle for the legend
end
end
end
legend(plotHandles, {'Instance 1', 'Instance 2', 'Instance 3', 'Instance 4', 'Instance 5'});
This way, your plot will look neat, and the legend will only show the instances, each with its respective color. Give it a try and tweak it as needed for your specific data!
John
on 31 Jan 2025
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
More Answers (1)
('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
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!
