13 - matlab m_map地學繪圖工具基礎函數 - 介紹創建管理顏色映射的函數m_colmap和輪廓圖繪制顏色條的函數m_contfbar
- 0. 引言
- 1. 關于m_colmap
- 2. 關于m_contfbar
- 3. 結語

0. 引言
?? 本篇介紹下m_map
中用于創建和管理顏色映射函數(m_colmap
)和 為輪廓圖繪制顏色條的函數(m_contfbar
)。
1. 關于m_colmap
??m_colmap
函數用于創建和管理顏色映射(colormap)。顏色映射可以用來對數據進行可視化編碼,例如在地圖上顯示溫度、高度、速度等信息。
m_colmap
函數的一般形式如下:
cols=m_colmap(nme,m,ncol)
% m_colmap Useful colormaps
% m_colmap(NAME) returns an M-by-3 matrix containing the NAME colormap
% where NAME is one of:
% 'jet' : a perceptually uniform variation of the JET colormap. It
% contains the multiple colours which make JET useful, while
% avoiding the weird highlighting especially around yellow and
% cyan. The colors begin with dark blue, range through shades
% of blue, green, orange and red, and ends with dark red.
% 'mBOD' : a modified blue/orange diverging colormap without white,
% useful as a colorblind-friendly jet alternative
%
% 'diverging' : a blue/red diverging colormap
% 'BOD' : a blue/orange diverging colormap
% 'rBOD' : a different blue/orange colormap with gray in the middle
%
% 'odv' : an isoluminant map
% 'cyclic2': a cyclic colormap (for angles) with two dark regions
% 'cyclic1': a cyclic colormap (for angles) with one dark region
%
% 'land' : a topographic height (green-brown-white) shading
% 'water' : blue shading for water (goes with 'land').
% 'gland' : a topographic height shading with more green
% 'bland' : a topographic height shading with browns only.
%
% 'blue' : a perceptually useful blue shading (good for bathymetry)
% 'green' : a perceptually useful green shading
% 'chlorophyll': Enhanced green for chlorophyll (to red)
% 'CBchlorophyll': Enhanced green for chlorophyll (to magenta)
% <-colorblind friendly
%
% 'EK80' : a standard echo-sounder map.
% 'chart': the standard chart colour (single color)
%
% and M is the same length as the current figure's colormap. If no
% figure exists, the length of the default colormap is used. The
% length can be explicitly specified with s_colmap(NAME,M).
%
% m_colmap('demo') demonstrates the colormaps.
%
% m_colmap(NAME,'step') returns a 256-color map in which colours are
% perceptually bunched into 16 separate colours. This is useful
% if you want to see "edges" in what would be an otherwise
% smooth gradation (i.e. approaching the look of contouring).
%
% m_colmap(NAME,'demo') Gives a demo of this behavior
%
% m_colmap(NAME,'step',M) bunches into M colours.
??其中,nme
選擇一種顏色類型;m
顏色長度,不設置就自動用默認的,設置了就顏色條按區間進行分段;
??m_map
的所有顏色類型可以通過下面一句代碼查看:
m_colmap demo

m_colmap
函數的使用示例,示例數據可以從網盤獲取,提取碼:ofxi:
%% m_colormap
clc;clear;
ncFilePath = 'GLDAS_NOAH10_M.A200602.021.nc4';
lon = ncread(ncFilePath,'lon');
lat = ncread(ncFilePath,'lat');
soilmoi_data = ncread(ncFilePath,'SoilMoi0_10cm_inst'); [LN,LT]=meshgrid(lon,lat);m_proj('mercator','long',[69.5 105.5],'lat',[24.5 40.5]);
% 在地圖上繪制偽彩色圖
m_pcolor(LN,LT, soilmoi_data');colormap( m_colmap('jet') );
%colormap( m_colmap('jet',10) );% 添加地圖邊界、標簽和色標
m_gshhs('ic','color',[.5 .5 .5]) % 中等分辨率海岸線
m_gshhs('ir2','color','k') % 中等分辨率河流
m_grid('linestyle','none','tickdir','out');
colorbar;
??對比下圖可以發現,繪制在圖右側的顏色調劃分上存在差異,這就是設置了m的差別,根據需要選擇合適的命令參數,能夠繪出更加符合的圖示:
![]() | ![]() |
2. 關于m_contfbar
??m_contfbar
函數用于添加色標,以顯示地圖上的等值線或填充的數據的顏色對應的數值范圍。
??m_contfbar
函數的一般形式是:
[ ax,h ] = m_contfbar(lon,lat,DATA,LEVELS)
% M_CONTFBAR Draws a colour bar for contourf plots
% M_CONTFBAR([X1,X2],Y,DATA,LEVELS) draws a horizontal colourbar
% between the normalized coordinates (X1,Y) and (X2,Y) where
% X/Y are both in the range 0 to 1 across the current axes.
% Negative values can be used to position outside the axis.
%
% Differences from COLORBAR are: the bar is divided into solid
% colour patches exactly corresponding to levels provided by
% CONTOURF(DATA,LEVELS) instead of showing the whole continuous
% colourmap, the parent axis is not resized, the axis can be made
% as large or small as desired, and the presence of values
% above/below contoured levels is indicated by triangular pieces
% (MATLAB 2014b or later).
%
% M_CONTFBAR(X,[Y1,Y2],...) draws a vertical colourbar
%
% The DATA,LEVELS pair can also be replaced with CS,CH where
% [CS,CH]=CONTOURF(DATA,LEVELS).
%
% M_CONTFBAR(...,'parameter','value') lets you specify extra
% parameter/value pairs for the colourbar in the usual handle-graphics
% way. Parameters you might set include 'xticks','xticklabels',
% 'xscale','xaxisloc' (or corresponding y-axis parameters for
% a vertical colourbar) and 'fontsize'.
%
% Additional parameter/value pairs allow special customization of the
% colourbar:
% 'axfrac' : width of the colourbar (default 0.03)
% 'endpiece' : 'yes' (default) or 'no' show triangular
% endpieces.
% 'levels' : 'set' (default) shows a colourbar with exactly
% the levels in the LEVELS argument.
% 'match' shows only the subsect of levels actually
% used in the CONTOURF call. E.g., if your data ranges
% from (say) 121 to 192, and LEVELS=[10:10:300],
% then only levels in 130:10:190 actually appear in both
% the CONTOURF and the colourbar.
% 'edgecolor' : 'none' removes edges between colors.
%
% [AX,H]=M_CONTFBAR(...) returns the handle to the axis AX and to
% the contourobject H. This is useful to add titles, xlabels, etc.
%
% M_CONTFBAR(BAX,...) where BAX is an axis handle draws the colourbar
% for that axis.
其中:
lon,lat
控制色標的位置,([lon1 lon2],[lat]…)表示橫向色標;([lon1],[lat1 lat2]…)縱向色標;DATA
進行繪圖的輸入數據;axfrac
設置色標寬度 設置value數值;endpiece
設置結束端,yes 或 no;edgecolor
色標框邊緣顏色,可以設置’r’ 、'g’等matlab內置顏色,也可以設置none;
m_contfbar函數的使用示例(在官網示例上不斷嘗試修改):
m_proj('lambert','lat',[5 24],'long',[105 125]);set(gcf,'color','w') % Set background colour before m_image call
caxis([-6000 0]);
colormap(flipud([flipud(m_colmap('blues',10));m_colmap('jet',118)]));
m_etopo2('shadedrelief','gradient',3);
m_gshhs_i('patch',[.8 .8 .8]);
m_grid('box','fancy');
ax=m_contfbar(.97,[.5 .9],[-6000 0],[-6000:100:000],'edgecolor','none','endpiece','no','axfrac ',0.1);
xlabel(ax,'meters','color','k');
??示例運行結果,上邊的可選參數可自行修改。

3. 結語
?? 本篇介紹了用于創建和管理顏色映射函數(m_colmap
)和 為輪廓圖繪制顏色條的函數(m_contfbar
),通過示例展示了各函數的基本用法,對于繪制常見地學圖已經夠用了,后面如果發現還有其它相關函數再進行補充。希望對繪圖的你有所幫助
😜
😜😜
😜😜😜😜