필터 지우기
필터 지우기

Plotting 2D curves with specific colors for certain curves

조회 수: 1 (최근 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 CenterFile Exchange에서 Instrument Control Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by