Colormaps for plotting lines whose values vary cyclically.

4 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Dave B
Dave B il 24 Feb 2024
Modificato: Dave B il 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')

Più risposte (0)

Categorie

Scopri di più su Colormaps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by