MATLAB Answers

editing the JET colormap

57 views (last 30 days)
Hans123
Hans123 on 25 Jun 2020
Commented: Hans123 on 29 Jun 2020
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?

  0 Comments

Sign in to comment.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 25 Jun 2020
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 Comments

Show 2 older comments
Hans123
Hans123 on 25 Jun 2020
Hi Bjorn, thank you again for the code.
Could you help me understand the logic behind your code?
imagesc(peaks(123)) %creates peak plot
cx = caxis; %I do not understand this
n_colors = 128 %number of colors
cmp = jet(n_colors); %create new cmap with jet array
colorbar %colorbar
colormap(cmp) %sets colormap to cmp
idx0 = round(1 + (n_colors-1)/diff(cx)*(0-cx(1))); % closest index to zero-level of current caxis
%I do not understand this at all^ or the lines that follow below
cmp(idx0,:) = 1;
colormap(cmp)
Bjorn Gustavsson
Bjorn Gustavsson on 26 Jun 2020
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 on 29 Jun 2020
Thank you, I really appreciate your help!

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 25 Jun 2020
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.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by