Colormaps for plotting lines whose values vary cyclically.

6 ビュー (過去 30 日間)
Divyaprakash
Divyaprakash 2024 年 2 月 24 日
コメント済み: DGM 2024 年 2 月 25 日
I have let's say 12 lines and each of these correspond to a different value which repeats periodically. In short it means that the 1st line and the 12th line have the same value. I want to plot these lines such that each of them have a separate color corresponding to the line's value and I want to display each of these colors in a colorbar. Is it possible to do?

採用された回答

Dave B
Dave B 2024 年 2 月 24 日
編集済み: Dave B 2024 年 2 月 24 日
You can do this using the colororder to control the coloring of lines, it has the modulo behavior built right into it. But using a colorbar to represent the colors takes a little extra work, because the colorbar is tied to the Axes colormap (i.e. it's intended for labeling continuous colors rather than discrete colors). It's easy enough to make them line up:
rng(1) % just for reproducibility of the example
% A sample dataset of 36 lines
vals = rand(100,36)+(1:36);
% Note that his is the same as looping over columns and plotting each one
plot(vals,'LineWidth',2)
% A palette of 12 colors
mycolors = rand(12,3);
% Set the colororder to make lines use the palette
colororder(mycolors)
% Fake out a continous colormap for the colorbar
% Using colorlimits is an easy way to make the tick values line up
clim([.5 12.5])
colormap(mycolors)
cbar=colorbar;
cbar.Ticks=1:12;
cbar.TickDirection='out'; % just because I thought it looked a tad better
Alternatively, if you wanted to use a legend to label the lines, it's a little easier:
clf
rng(1)
vals = rand(100,36)+(1:36);
myplots=plot(vals,'LineWidth',2);
mycolors = rand(12,3);
colororder(mycolors)
legend(myplots(1:12),string(1:12),'Location','eastoutside')
  2 件のコメント
Divyaprakash
Divyaprakash 2024 年 2 月 25 日
Thank you so much for your prompt help.
DGM
DGM 2024 年 2 月 25 日
See also the orderedcolors() tool, which does have two 12-color pallettes.

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

その他の回答 (0 件)

カテゴリ

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