How can I plot 18 lines in different colors on a single plot?
10 ビュー (過去 30 日間)
古いコメントを表示
採用された回答
DGM
2023 年 1 月 10 日
編集済み: DGM
2023 年 1 月 10 日
In versions prior to R2019b, you can always set the axes property without the colororder() function.
% some fake data
x = linspace(0,1,100);
y = bsxfun(@plus,rand(18,100),(1:18).'); % i'm assuming we're in an older version
% set the axes colororder
CT = jet(18); % not that this is a good choice
set(gca,'colororder',CT);
% plot things
hold on
plot(x,y)
Of course, like @Bjorn Gustavsson says, unless you do something beyond this (e.g. using a carefully crafted map or using other line properties), putting 18 curves in the same plot can be a readability problem, especially if they overlap or the results need to be printed. For example:
clf
% exact same colors, but data series don't have sequential offsets
x = linspace(0,1,100);
os = (1:18).';
y = bsxfun(@plus,rand(18,100),os(randperm(18)));
% set the axes colororder
CT = jet(18); % not that this is a good choice
set(gca,'colororder',CT);
hold on
hp = plot(x,y);
% which curve is data 4?
legend(hp,'location','eastoutside')
See also:
0 件のコメント
その他の回答 (2 件)
Bjorn Gustavsson
2023 年 1 月 10 日
For that type of tasks I have had good use of the cmlines-function in the colormap-and-colorbar-utilities toolbox on the file exchange. With that it is as simple as:
ph = plot(randn(18,18));
colormap(turbo)
cmlines(ph(1:12))
colormap(copper)
cmlines(ph(13:18))
(unasked for advice: sometimes my plots become more legible if I also use linewith and linestyle to differentiate between lines)
HTH
0 件のコメント
the cyclist
2023 年 1 月 10 日
編集済み: the cyclist
2023 年 1 月 10 日
If your version of MATLAB is 2019b or later, I suggest the colororder function. (I see that you mention an older version, so this may be more for other folks who come across this question.)
And for that many colors, I would recommend using a utility like this one for defining maximally distinguishable colors (and/or using different plotting symbols and line styles.)
1 件のコメント
Bjorn Gustavsson
2023 年 1 月 10 日
Hopefully there are some more "natural" ordering and/or grouping of the function to plot, and then it might be way better to one distinct colormap per group of curves to guide the viewer.
参考
カテゴリ
Help Center および File Exchange で Surface and Mesh Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!