MATLAB:使用自定义颜色图绘制栅格图
MATLAB: Plot raster map with custom colormap
在 MATLAB 中,我有一个与引用对象 R 关联的矩阵 map_data(均在 this MAT-file 中)。我想用一个离散的颜色条来映射它,给定一个不规则的值范围,看起来像这样:
我想使用 geoshow()
或类似的东西,让我可以随意重新投影并将 shapefile 叠加在栅格上。但真的任何能让我走上正轨的东西都会非常感激。
我正在使用 MATLAB r2014b。这是颜色图的相关信息:
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 答案中交叉发布。
MATLAB 仅 built-in 支持线性色图。因此,对于像这样的非线性映射,您需要转换 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;
结果如下:
使用 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 ;
在 MATLAB 中,我有一个与引用对象 R 关联的矩阵 map_data(均在 this MAT-file 中)。我想用一个离散的颜色条来映射它,给定一个不规则的值范围,看起来像这样:
我想使用 geoshow()
或类似的东西,让我可以随意重新投影并将 shapefile 叠加在栅格上。但真的任何能让我走上正轨的东西都会非常感激。
我正在使用 MATLAB r2014b。这是颜色图的相关信息:
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 答案中交叉发布。
MATLAB 仅 built-in 支持线性色图。因此,对于像这样的非线性映射,您需要转换 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;
结果如下:
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 ;