Colormaps for plotting lines whose values vary cyclically.

6 Ansichten (letzte 30 Tage)
Divyaprakash
Divyaprakash am 24 Feb. 2024
Kommentiert: DGM am 25 Feb. 2024
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?

Akzeptierte Antwort

Dave B
Dave B am 24 Feb. 2024
Bearbeitet: Dave B am 24 Feb. 2024
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 Kommentare
Divyaprakash
Divyaprakash am 25 Feb. 2024
Thank you so much for your prompt help.
DGM
DGM am 25 Feb. 2024
See also the orderedcolors() tool, which does have two 12-color pallettes.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Colormaps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by