Plotting 2D curves with specific colors for certain curves

3 vues (au cours des 30 derniers jours)
H R
H R le 7 Sep 2021
Commenté : H R le 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 commentaires
Kevin Holly
Kevin Holly le 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 le 7 Sep 2021
That's awesome thanks!

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 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;

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by