editing the JET colormap

38 Ansichten (letzte 30 Tage)
Hans123
Hans123 am 25 Jun. 2020
Kommentiert: Hans123 am 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?

Akzeptierte Antwort

Bjorn Gustavsson
Bjorn Gustavsson am 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 Kommentare
Bjorn Gustavsson
Bjorn Gustavsson am 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 am 29 Jun. 2020
Thank you, I really appreciate your help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Steven Lord
Steven Lord am 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.

Community Treasure Hunt

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

Start Hunting!

Translated by