채택된 답변

Ayush Anand
Ayush Anand 2024년 4월 4일

0 개 추천

Hi,
You can use the "hsv" function to create a cyclic rainbow color palette, as the HSV color space is naturally cyclic with respect to hue.
% Number of colors you want in the palette
numColors = 256;
% Generate a cyclic rainbow color palette
rainbowPalette = hsv(numColors);
% Example: Use the palette for a plot
x = linspace(0, 2*pi, 1000);
y = sin(x);
scatter(x, y, 10, mod(1:1000, numColors)+1, 'filled');
colormap(rainbowPalette);
colorbar;
In this example, "hsv(numColors)" generates a "numColors x 3" matrix, where each row is an RGB color corresponding to a color in the HSV color wheel. The colors range from red, through all the hues of the rainbow, back to red, creating a cycle.
You can read more about the "hsv" function here: https://www.mathworks.com/help/matlab/ref/hsv.html
Hope this helps!

댓글 수: 4

Guy Nir
Guy Nir 2024년 4월 4일
편집: Guy Nir 2024년 4월 4일
Yes, I was thinking of hsv too but is it ‘really’ rainbow? Because it seems to be more red in one end and ‘brownish’ at the other end. But that could be me…
It depends on the number of elements in the colormap. The way hsv is interpolated, the last color should wrap to the first color with the same interval as any other two adjacent colors in the colormap. If there are few colors in the hsv colormap, the wrapping may not be obvious.
tiledlayout('horizontal','TileSpacing','tight')
for i = [3:15,20:5:40]
ax = nexttile();
colormap(ax,hsv(i))
cb = colorbar();
axis off
cb.Title.String = string(i);
cb.Ticks = [];
end
Guy Nir
Guy Nir 2024년 4월 4일
I see. I think that ideally the function will take a map similiar to the one with 40 colors and rescale it with let's say, 20 colors, thus keeping the obvious cyclic nature of the map.
If you want to always ensure that the ends are the same color regardless of the length of the map, you can just append that tuple.
ncolors = 9;
CT = hsv(ncolors);
CT = CT([1:end 1],:);
image(permute(CT,[1 3 2]))
set(gca,'ydir','normal')
... of course this makes anything you do with the map ambiguous, since the same color maps to two distinct values.
Despite casual appearances, it's also no longer cyclic, since it has a duplicate red element.
image(permute([CT;CT],[1 3 2]))
set(gca,'ydir','normal')
This is something that might be more justifiable with a really long colormap, but for a short discrete color table, I don't think it makes sense.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Color and Styling에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2024년 4월 4일

댓글:

DGM
2024년 4월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by