how to generate colors for line plot

130 ビュー (過去 30 日間)
Yu Li
Yu Li 2018 年 11 月 26 日
編集済み: Vencel Kozma 2022 年 12 月 5 日
I have a set of lines to plot in one figure. the first line is experimental data so I want to plot with 'black', the result are simulation result, I would like to plot with different colors.
the problem is that, the number of these simulation result is not clear, some times, only 2, some times, about 10.
what I did before is to determine some customized color:
cm{1}='r';
cm{2}='b';
cm{3}='m';
then,
for j=1:1:length(P)
P_tmp=P{j};
plot(P_tmp(:,1),P_tmp(:,2),['-',cm{j}]);
hold on
end
however, the color names to determine cm is limited, when number of plot are large, I do not have enough color names to customize parameter 'cm'.
is there any way to solve this problem?
Thanks!
Yu

回答 (2 件)

Jim David
Jim David 2018 年 11 月 26 日
Different colors for an indefinite number of plots can be achieved by using the rand function to set the RGB values for the color for the plot. Here is an example that demonstrates how this can be done.
x = linspace(0,10,150);
y = cos(5*x);
figure
hold on
for i = 1:10
y = i*cos(i*x);
plot(x,y,'Color',[rand,rand,rand])
end
hold off
A further point of interest could be MATLAB's default ColorOrder. Here is the documentation for the same:
This could be used in conjunction with the rand function to increase the number of colors MATLAB cycles through.

Vencel Kozma
Vencel Kozma 2022 年 12 月 5 日
編集済み: Vencel Kozma 2022 年 12 月 5 日
I also tried this, a deterministic way to create colour. That being said, the number of colours is limited:
Colmat = hsv(); % hsv---> Colourmap, which has a wide range of colours.
figure()
for k = 1:length(Data)
Colour = Colmat( mod(7*(k-1), 64)+1, :)... ---> Colour
*(0.9-0.3*( mod(floor(7*(k-1)/64), 3) )) % --->Brightness
% Insert plot here!
end

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by