editing the JET colormap

37 次查看(过去 30 天)
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 中查找有关 Blue 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by