MATLAB Answers

How to make a colorbar with single color with varying intensity?

2 views (last 30 days)
Subhodh Sharma
Subhodh Sharma on 9 Aug 2021
Answered: David Goodmanson on 10 Aug 2021
I have a data whose max is 1.5 and min is -0.5. This is a 2d data. I want to make a colorbar with single color for the contourf plot. Min_scale_colorbar= light blue; Max_Scale_colorbar=intense blue.

Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 9 Aug 2021
Perhaps the ice colormap from cmocean-perceptually-uniform-colormaps (or parts of that one or modifications of any oth the other colormaps that tool provides), or something from any of the range of colormap-tools you find on the file exchange:
crameri-perceptually-uniform-scientific-colormaps, pycolormap4matlab, cubehelix-colormap-generator-beautiful-and-versatile, pridemap, colorbrewer-attractive-and-distinctive-colormaps, custom-colormap for example. Or you could simply make your own colormap as a suitably bright-blue to darker-blue n-by-3 array with the first two columns for the red and green RGB-values lower than the third blue channel (all values between 0 and 1, you know best what fits your sensibilities...)
HTH
  2 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 9 Aug 2021
OK, but this is just an example to get you going and not my claim to make a nice blue-blue colormap...
cmap_blue = 1-gray; % (grayscale from white in the low-end to black in the high end)
subbplot(3,1,1:2)
imagesc(peaks(234)),colorbar, % just something to illustrate the colourmap with
colormap(cmap_blue)
subplot(3,1,3)
rgbplot(cmap_blue)
cmap_blue(:,3) = 0.5*cmap_blue(:,3) + 0.5;
colormap(cmap_blue)
rgbplot(cmap_blue)
cmap_blue(:,1:2) = cmap_blue(:,1:2).^2*0.8;
colormap(cmap_blue)
subplot(3,1,3)
rgbplot(cmap_blue)
Then you can continue to fidget around with the three (r, g and b) colourmap-curves to adjust it to your hearts content - this is a risky business since one can get a bit consumed with creating "the prittiest of them all" which will take much more time than it is worth in improvements...
...that's why it I find it to be preferable to use some of the already designed colormaps in one of the toolboxes on the file-exchange or from the default matlab-colormaps.

Sign in to comment.


David Goodmanson
David Goodmanson on 10 Aug 2021
Hi Subhodh,
I agree with Bjorn that it's possible to get caught up in this, and it is going to take some work to make something nicer looking than
surf(peaks(40))
colormap('cool')
colorbar
But here is a way to at least mess around in less time. With the colorwheel function below you can display a color wheel and then use the datatips feature on the plot to pick some rgb point near the edge. Then convert rgb to hue, saturation and value. For a point on the edge, every color has saturation = 1 and hue is what ever you like. For simplicity I used value (i.e. brightness) = 1 all the time and did not vary it.
The code below uses constant hue and varies the saturation (from .1 to 1 in this example) to make the color map. Obviously you can pick two points on the color wheel in lots of different ways and interpolate between those to make the color map.
colorwheel(1,2)
% use datatips feature to select a color
rgbcols = [0 .5 1]; % for example
h = rgb2hsv(rgbcols); % go to hue, sat, val
n = 64;
satmin = .1;
map = hsv2rgb([h(1)*ones(n,1) linspace(satmin,1,n)' ones(n,1)])
figure(1)
colormap(map)
colorbar
function colorwheel(val,fignum)
% colorwheel for value val (of hue,saturation,value), displayed in
% figure fignum. If no inputs are provided, value = 1 fignum = 1.
% Hue and saturation vary. Saturation runs from 0 at the center
% to 1 on the perimeter. Hue starts at 0 on the right x axis
% and goes around counterclockwise to value 1 on the right x axis
if nargin == 0
val = 1;
fignum = 1;
end
n = 400;
nn = (-n:n)/n;
[x y] = meshgrid(nn,nn);
huu = angle(-x+i*y)/(2*pi) + 1/2;
sat = abs(x+i*y);
val = val*ones(size(huu));
% background gray color
ind = sat>1;
huu(ind) = 1;
sat(ind) = 0;
val(ind) = .8;
hsv = [huu(:) sat(:) val(:)];
rgb = hsv2rgb(hsv);
rgb = reshape(rgb,2*n+1,2*n+1,3);
fig(fignum)
imagesc(rgb)
axis square
grid off
end

Community Treasure Hunt

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

Start Hunting!

Translated by