Plot raster map with custom colormap

8 views (last 30 days)
Sam
Sam on 11 Jan 2016
Commented: Chad Greene on 21 Jun 2018
I have a matrix map_data associated with referencing object R (both in attached MAT-file). I want to map it with a discrete colorbar given an irregular range of values to look something like this:
How can I do this using geoshow or something else?
I'm using MATLAB r2014b. Here is the relevant information for the colormap:
R G B
0 <= map_data < 0.001 204 204 204
0.001 <= map_data < 0.005 153 153 153
0.005 <= map_data < 0.01 255 255 178
0.01 <= map_data < 0.05 254 204 92
0.05 <= map_data < 0.1 253 141 60
0.1 <= map_data < 0.25 240 59 32
0.25 <= map_data < 0.5 189 0 38
0.5 <= map_data < 1 0 0 0
Thanks in advance.

Answers (3)

Sam
Sam on 12 Jan 2016
Thanks to Will at Stack Overflow , who suggested using histc(). This is still not using the Mapping Toolbox though, which would be nice.
my_colormap = [204 204 204
153 153 153
255 255 178
254 204 92
253 141 60
240 59 32
189 0 38
0 0 0]/255 ;
binEdges = [0 0.001 0.005 0.01 0.05 0.1 0.25 0.5 1] ;
labels = textscan(num2str(binEdges*100),'%s') ;
labels = labels{1} ;
labels{length(labels)} = [labels{length(labels)} '%'] ;
[~,indices] = histc(map_data,binEdges);
indices(isnan(map_data)) = NaN ;
indices(isinf(map_data)) = NaN ;
figure ;
pcolor(indices-1) % Instead of image(), to display NaN values as white
shading flat
axis equal tight
colormap(gca,my_colormap); % gca as first argument prevents
% colormap from changing for all
% subsequent plots
h = colorbar;
caxis([0 length(binEdges)-1])
h.YTickLabel = labels ;

Chad Greene
Chad Greene on 12 Jan 2016
I take philosophical issue with binning values from a continuous range unless those exact bins are used as discrete categories for your work. I don't know anything about the application, but as a general rule of thumb I say let continuous variables be depicted by continuous colormaps. Perhaps plot in log scale? Here's my stab at it. I couldn't load your referencing matrix R so I made up a grid that seems approximately right. Below I'm using Stephen Cobeldick's brewermap function and my rgb function to define colors.
% Load data:
load 'matlab_20160111.mat';
% Initialize map:
worldmap('world')
geoshow('landareas.shp','facecolor',0.8*[1 1 1])
% Plot gridded data:
[lon,lat] = meshgrid(-179:2.5:179,-89:2:89);
pcolorm(lat,lon,log10(100*map_data));
% Set colorbar with log10 scale:
caxis([0 2])
cb = colorbar;
set(cb,'ytick',log10([1 5 10 50 100]),...
'yticklabel',num2str([1 5 10 50 100]'))
ylabel(cb,'percentage of some variable')
% Define colormap and background color:
colormap(brewermap(256,'reds'))
setm(gca,'ffacecolor',rgb('ocean blue'))
  2 Comments
Mahmoud Solomon
Mahmoud Solomon on 10 Jun 2018
I followed this procedure and was able to generate this map. However, I have tried manipulating the code to plot only for specific region but this has not work. Can you please help me out?
Chad Greene
Chad Greene on 21 Jun 2018
Mahmoud: Try specifying the map region when you call worldmap.

Sign in to comment.


Walter Roberson
Walter Roberson on 10 Jun 2018
See https://www.mathworks.com/matlabcentral/answers/377542-how-can-i-change-number-on-colorbar-caxis#answer_300478 to see how to create a colormap with the desired properties without binning the data itself.
  1 Comment
Mahmoud Solomon
Mahmoud Solomon on 11 Jun 2018
Thanks for your response but I was talking about the plot itself and not the colorbar. I want to plot on the map but for a specific region and not the entire map.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!