Plotting 2D curves with specific colors for certain curves

7 ビュー (過去 30 日間)
H R
H R 2021 年 9 月 7 日
コメント済み: H R 2021 年 9 月 7 日
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
H R
H R 2021 年 9 月 7 日
That's awesome thanks!

サインインしてコメントする。

採用された回答

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 Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by