필터 지우기
필터 지우기

Colormaps for plotting lines whose values vary cyclically.

조회 수: 2 (최근 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 CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by