Plotting 2D curves with specific colors for certain curves

3 次查看(过去 30 天)
Hi all. I have 1000 simple 2D curves (data = rand(1000,20)). X axis for all is from (x=1:20). Each curve has an index between 1 to 7 (idx=randi([1 7], 1000,1)) . How can I quickly plot these curves alltogether in a single plot such that the curves with similar idx share the same color?
Thank you.
  4 个评论
Kevin Holly
Kevin Holly 2021-9-7
data = rand(1000,20);
x=1:20;
idx=randi([1 7], 1000,1);
color = ["r" "g" "b" "m" "c" "y" "k"];
Different colors
p = plot(x,data);
for i = length(p):-1:1
if idx(i) == 1
p(i).Color = "r";
end
if idx(i) == 2
p(i).Color = "g";
end
if idx(i) == 3
p(i).Color = "b";
end
if idx(i) == 4
p(i).Color = "m";
end
if idx(i) == 5
p(i).Color = "c";
end
if idx(i) == 6
p(i).Color = "y";
end
if idx(i) == 7
p(i).Color = "k";
end
end
Separate
figure
for i = 1:length(data)
subplot(7,1,idx(i))
plot(x,data(i,:),color(idx(i)));
hold on
end

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2021-9-7
Try this:
numCurves = 1000;
data = rand(numCurves,20);
% X axis for all is from
x = 1 : 20;
% Each curve has an index between 1 to 7
idx = randi([1 7], numCurves, 1);
customColorMap = jet(max(idx));
for k = 1 : size(data, 1)
thisColor = customColorMap(idx(k), :);
fprintf('Drawing line %d in [%.3f, %.3f, %.3f].\n', ...
k, thisColor(1), thisColor(2), thisColor(3));
plot(x, data(k, :), '-', 'Color', thisColor, 'LineWidth', 2);
hold on;
% Occasionally refresh the screen.
if rem(k, 50)
drawnow;
end
end
grid on;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by