editing the JET colormap

조회 수: 21 (최근 30일)
Hans123
Hans123 2020년 6월 25일
댓글: Hans123 2020년 6월 29일
I want to edit the jet colormap such that it reflects that values of 0 are shown in white, I followed the below code and the following figure was obtained
a=colormap(jet)
n = length(a)/2;
a(n,:)=[1 1 1]; %replaces the midpoint with a white line
...
%plot...
%colorbar...
I found out the midpoint of the colormap does not necessarily mean the midpoint I am looking for - i.e. 0. my question is how can I tweak the above code to give me a white line when the value of the colorbar = 0?

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2020년 6월 25일
You should be able to do something like this:
imagesc(peaks(123))
cx = caxis;
n_colors = 128
cmp = jet(n_colors);
colorbar
colormap(cmp)
idx0 = round(1 + (n_colors-1)/diff(cx)*(0-cx(1))); % closest index to zero-level of current caxis
cmp(idx0,:) = 1;
colormap(cmp)
HTH
  댓글 수: 5
Bjorn Gustavsson
Bjorn Gustavsson 2020년 6월 26일
OK step by step.
cx = caxis;
Is just getting the intensity-limits of the colour-scale, you get a 1 x 2 array out with [cmin, cmax].
The line
idx0 = round(1 + (n_colors-1)/diff(cx)*(0-cx(1)));
Is just the equation for a straight line on the form
if you see the limits of the intensity-range as and and the end-indices of the colormap (1 and n_colors) as and . Then the "fractional index" closest to zero should be what we get when plugging 0 into . But we have to round that.
cmp(idx0,:) = 1;
We simply set all 3 components of the colormap-array on row idx0 to one (white). You can set it to any [r g b] as long as all three are between 0 and 1.
Then we set the colormap of the current figure to that colormap
Hans123
Hans123 2020년 6월 29일
Thank you, I really appreciate your help!

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

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 6월 25일
You should probably read through this documentation page and the pages listed in the "Related Topics" section on that page. It talks about how MATLAB maps your data onto the colormap.
If you want a white line around where your data is zero, I wouldn't necessarily adjust the colormap for that. I'd probably plot the surface with all my data then add one contour or contour3 at level 0. The LineSpec input to contour or contour3 lets you make the line white and the LineWidth name-value pair argument lets you make the line thinner or thicker if you need it to stand out more clearly.

카테고리

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