Filter löschen
Filter löschen

Plotting 2D curves with specific colors for certain curves

1 Ansicht (letzte 30 Tage)
H R
H R am 7 Sep. 2021
Kommentiert: H R am 7 Sep. 2021
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 Kommentare
Kevin Holly
Kevin Holly am 7 Sep. 2021
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 am 7 Sep. 2021
That's awesome thanks!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 7 Sep. 2021
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;

Weitere Antworten (0)

Kategorien

Mehr zu Instrument Control Toolbox finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by