way to legend a data organized into colours in simple plot?

2 次查看(过去 30 天)
first_data = [[3 4 8 1];[3 6 4 9]]; % desired legend (first) & color (green)
second_data = [[1 3 5];[3 2 7];[3 4 2]]; % desired legend (second) & color (cyan)
combine = {first_data,second_data}; % recieving new data in every 'for' loop iteration thats why combining
colours = {'g','c'};
Legend_Names = {'first','second'};
for i = 1:length(combine)
plot(cell2mat(combine(i)),'Color',cell2mat(colours(i)),'LineWidth',2); grid on; hold on;
end
legend(Legend_Names)
i wish to plot my each data set with same colour... in this case legend ''Second'' should have ''cyan'' colour

采纳的回答

Star Strider
Star Strider 2025-2-26
编辑:Star Strider 2025-2-26
I am not certain what you want.
One option —
first_data = [[3 4 8 1];[3 6 4 9]]; % desired legend (first) & color (green)
second_data = [[1 3 5];[3 2 7];[3 4 2]]; % desired legend (second) & color (cyan)
combine = {first_data,second_data}; % recieving new data in every 'for' loop iteration thats why combining
colours = {'g','c'};
Legend_Names = {'first','second'};
for i = 1:length(combine)
hp{i} = plot(cell2mat(combine(i)),'Color',cell2mat(colours(i)),'LineWidth',2, DisplayName=Legend_Names{i});
hold on;
end
grid on;
legend([hp{1}(1) hp{2}(1)], Location = 'best')
For either of these, it is necessary to return the handles of each plot call, and pass them to the legend call..
EDIT — Ran code again.
.
  5 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

标签

产品


版本

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by