manually enter legend details
显示 更早的评论
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 ?
采纳的回答
更多回答(1 个)
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
类别
在 帮助中心 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
