Colormaps for plotting lines whose values vary cyclically.
4 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!