manually enter legend details
Show older comments
I have 9 curves on a plot. 3 different colors x 3 (solid lines, dotted lines etc). However Instead of crowding my legend with 9 entries, I'd like to depict each color - their meaning (example red - 30-70Hz, blue - 20-50hz etc) and separately the meaning of dotted lines (training set), solid lines (test set) etc. How do I do this in Matlab ?
Accepted Answer
More Answers (1)
David Angeles
on 24 Sep 2021
Edited: David Angeles
on 24 Sep 2021
Use the legend() function at the end of your plot with and empty string entry. Then, plot empty lines according to the final legend output you desire. Use 'DisplayName' to properly name the lines on the final legend output.
x = linspace(0,1, 10);
cols = ["red", "blue", "green"];
line_type = ["-", "--", ":"];
for i = 1:9
y = x + i/3;
col_i = cols(mod(i,3) + 1);
line_type_i = line_type(mod(i,3) + 1);
plot(x,y, line_type_i, 'Color', col_i)
hold on
end
legend('','Location', 'eastoutside')
col_names = ["30-70Hz", "20-50Hz", "40-80hz"];
for j =1:length(col_names)
plot([NaN NaN], [NaN NaN], 'Color', cols(j), 'DisplayName', col_names(j))
end
line_names = ["training set", "testing set", "simulation"];
for j =1:length(line_type)
plot([NaN NaN], [NaN NaN],line_type(j), 'Color', 'k', 'DisplayName', line_names(j))
end
hold off
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!