Plot two set of curves with the same colours on the same figure

조회 수: 14 (최근 30일)
carlos g
carlos g 2022년 7월 12일
댓글: Ishaan Mehta 2022년 7월 12일
I have two sets of curves plotted with a loop:
for ....
plot(den_avg,(z),'-','LineWidth',2) %Set 1
hold on
plot(velu_avg+1,(z),'--','LineWidth',2) %Set 2
hold on
end
I basically want sets 1 and 2 to have the same colours, this is:
First iteration: Plot 1 blue colour, Plot 2 blue colour
Second iteration: Plot 1 red colour, Plot 2 red colour
etc
How to do this?

채택된 답변

Ishaan Mehta
Ishaan Mehta 2022년 7월 12일
편집: Ishaan Mehta 2022년 7월 12일
Hi Carlos
I understand that you want to plot two curves on the same figure, and wish to change the plot color for each iteration of the for loop.
You can use MATLAB's rand function to create a random integer during each iteration, and use this random value to specify the color.
Here is a code snippet for the same:
t = 0:0.01:8;
colors = ["red", "blue", "green", "cyan", "magenta", "yellow", "black"];
numel(colors)
ans = 7
for i = 1:10
plotColor = colors(rem(i, numel(colors)) +1);
hold on
% printing sin and cos waveforms for demo
plot(t, sin(t + (0.2 * i)), 'color', plotColor);
plot(t, cos(t + (0.2 * i)), 'color', plotColor);
hold off
end
Hope it helps
Ishaan Mehta
  댓글 수: 2
carlos g
carlos g 2022년 7월 12일
Hi Ishaan,
Is it possible to replace randomColor by the actual by-default MATLAB colours? Namely blue, red, etc
Ishaan Mehta
Ishaan Mehta 2022년 7월 12일
Hey Carlos
Yes, that's possible too. I have updated my answer to use color codes for red, blue etc., please have a look.
You can also take a look at Specify Plot Colors documentation page for more color options.
Ishaan

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by