Clear Filters
Clear Filters

How to customize color map

9 views (last 30 days)
Gulraiz Safdar
Gulraiz Safdar on 27 Apr 2020
Answered: Olawale Ikuyajolu on 28 Apr 2020
Hi I want to plot temperature and precipiation anomlies from positive to negative values. I plotted that using pcolorm :
But the problem is I want to plot anomaly = 0 to be White not green. So positive anomaly should be in yellow orange and red and the negative anomaly in bleu colors.
This is how my teacher did and It should look like this:
Also I only want to plot regions between lattitude 30 N to 30 S so how can I do this?
Thnaks

Answers (2)

Image Analyst
Image Analyst on 28 Apr 2020
Try this. First we figure out what the colormap looks like using the sample image. Then we know how to build it and we can use linspace(), ones(), and zeros() to build a perfect one that we can apply to any image.
% Let see what this map looks like by examining the sample image.
hFig = figure;
fontSize = 20;
rgbImage = imread('image.png');
subplot(3, 1, 1);
imshow(rgbImage);
axis('on', 'image');
impixelinfo
redMap = rgbImage(18:122, 280, 1);
greenMap = rgbImage(18:122, 280, 2);
blueMap = rgbImage(18:122, 280, 3);
plot(redMap, 'r-', 'LineWidth', 2);
hold on;
plot(greenMap, 'g-', 'LineWidth', 2);
plot(blueMap, 'b-', 'LineWidth', 2);
legend('Red', 'Green', 'Blue', 'Location', 'southeast');
grid on;
title('Colormap from Sample image', 'FontSize', fontSize);
% Synthesize a similar "perfect" colormap.
% You don't need the sample image anymore - that was just to figure out how we could build this colormap.
numColors = 64; % An even number!
c4 = numColors/4;
redMap = [linspace(125, 255, c4), 255 * ones(1, c4), linspace(255, 0, c4), zeros(1, c4)] / 255;
greenMap = [zeros(1, c4), linspace(0, 255, c4), linspace(255, 0, c4), zeros(1, c4)] / 255;
blueMap = fliplr(redMap);
cMap = [redMap(:), greenMap(:), blueMap(:)];
subplot(3, 1, 2);
plot(redMap, 'r-', 'LineWidth', 2);
hold on;
plot(greenMap, 'g-', 'LineWidth', 2);
plot(blueMap, 'b-', 'LineWidth', 2);
legend('Red', 'Green', 'Blue', 'Location', 'southeast');
grid on;
title('Regenerated Colormap', 'FontSize', fontSize);
% Read in a gray scale image that we can apply the colormap to.
grayImage = imread('cameraman.tif');
subplot(3, 1, 3);
imshow(grayImage);
axis('on', 'image');
impixelinfo
title('Regenerated Colormap', 'FontSize', fontSize);
% Apply it to the image.
colormap(cMap);
colorbar;
hFig.WindowState = 'maximized';
fprintf('Done running %s.m.\n', mfilename);

Olawale Ikuyajolu
Olawale Ikuyajolu on 28 Apr 2020
r = -8 + (8+4)*rand(10); %generate random number between -8 and 4
pcolor(r);shading interp;
%% what you need starts from here
caxis([-8 4]) % set your colorbar limits
myColorMap = jet(24); %Each number takes two color values (we have 12 points between -8 and 4)
colorbar
myColorMap(16:17,:) = 1; % set the color around zero to white
colormap(myColorMap)

Community Treasure Hunt

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

Start Hunting!