MATLAB:使用自定义色彩映射绘制栅格地图

时间:2016-01-11 17:25:52

标签: matlab colors geospatial raster

在MATLAB中,我有一个与引用对象R相关联的矩阵map_data(都在this MAT-file中)。我想用一个离散的颜色条来映射它,给出一个不规则的值范围,看起来像这样:

enter image description here

我想使用geoshow()或类似的东西,允许我随意重新投影,并在栅格顶部叠加shapefile。但真正让我走上正轨的任何事情都会受到赞赏。

我使用的是MATLAB r2014b。以下是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

在MATLAB的答案中交叉发布。

2 个答案:

答案 0 :(得分:1)

MATLAB仅内置支持线性色彩映射。因此,对于像这样的非线性映射,您需要转换map_data的值,以便颜色的变化均匀分布。对于像这样的离散色图,整数索引是理想的,您可以使用histc轻松获取它们:

ranges = [0 0.001 0.005 0.01 0.05 0.1 0.25 0.5 1];
[~,ind] = histc(map_data,ranges);

使用ind中的索引代替map_data中的值作为颜色数据,然后您只需将指定的颜色应用为色彩映射。如果您需要在颜色条上标记真实的map_data值,请手动重新标记相应颜色条的YTickLabel

我没有使用geoshow来演示此映射工具箱,但显示为简单图像的工作原理如下:

image(ind)
axis equal tight
set(gca,'YDir','normal')
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);
h = colorbar;
h.YTickLabel = edges(h.YTick)*100;

结果如下:

map_data with discrete colormap

答案 1 :(得分:1)

Will had a great idea使用histc(),但我必须编辑他的代码才能让它适合我。这就是我最终的目标。

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 ;

Resulting plot

相关问题