Changing color and style using a loop

1 回表示 (過去 30 日間)
Hems
Hems 2018 年 2 月 8 日
コメント済み: Hems 2018 年 2 月 9 日
Hi, I want to plot 31 curves with varying colors and styles. My code is as follows:
colorspec = colormap(jet(8));
set(groot,'defaultAxesColorOrder',colorspec,'defaultAxesLineStyleOrder','-|--|:|-.');
for k1 = 1:31
semilogx(All_112(:,1), All_112(:,k1+1), 'Linewidth',2);
legendInfo{k1} = ['Day = ' num2str(k1)];
hold all
end
legend(legendInfo);
I am getting all the curves but Matlab plots one style first with all the 8 colors and then goes to the next. Is there any way to modify the code such that Matlab uses each color in the ColorOrder with all the line styles in LineStyleOrder and then jumps to the next color?
Thanks.

採用された回答

Walter Roberson
Walter Roberson 2018 年 2 月 8 日
"Is there any way to modify the code such that Matlab uses each color in the ColorOrder with all the line styles in LineStyleOrder and then jumps to the next color?"
No. You cannot use the default color and style mechanisms for this. You will need to do something like,
colorspec = colormap(jet(8));
styles = {'-', '--', ':', '-.'};
ncolor = size(colorspec,1);
nstyles = length(styles);
for k1 = 1 : 31
coloridx = 1 + floor((k1-1)/nstyles);
styleidx = 1 + mod(k1-1, nstyles);
semilogx(All_112(:,1), All_112(:,k1+1), styles{styleidx}, 'Linewidth', 2, 'Color', colorspec(coloridx,:));
legendInfo{k1} = ['Day = ' num2str(k1)];
hold all
end
  1 件のコメント
Hems
Hems 2018 年 2 月 9 日
Thank you very much Walter.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-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