示例代码
以下示例展示了 M_Map 的各项绘图能力。代码可直接在 MATLAB 中运行(凡引用本地数据文件的示例, 需自行替换为相应文件路径)。
1. M_Map 标志
m_proj('ortho','lat',48,'long',-123);
m_coast('patch','r');
m_grid('linest','-','xticklabels',[],'yticklabels',[]);
patch(.55*[-1 1 1 -1],.25*[-1 -1 1 1]-.55,'w');
text(0,-.55,'M\_Map','fontsize',25,'color','b',...
'verticalalignment','middle','horizontalalignment','center');
2. 北美地形的兰伯特等角圆锥投影
m_proj('lambert','long',[-160 -40],'lat',[30 80]);
m_coast('patch',[1 .85 .7]);
m_elev('contourf',[500:500:6000]);
m_grid('box','fancy','tickdir','in');
colormap(flipud(copper));
3. 北极地区的立体投影
% Note that coastline is drawn OVER the grid because of the order in which
% the two routines are called
m_proj('stereographic','lat',90,'long',30,'radius',25);
m_elev('contour',[-3500:1000:-500],'edgecolor','b');
m_grid('xtick',12,'tickdir','out','ytick',[70 80],'linest','-');
m_coast('patch',[.7 .7 .7],'edgecolor','r');
4. 世界大洋的两种断裂投影
subplot(211);
Slongs=[-100 0;-75 25;0 45; 25 145;45 100;145 295;100 295];
Slats= [ 8 80;-80 8; 8 80;-80 8; 8 80;-80 0; 0 80];
for l=1:7
m_proj('sinusoidal','long',Slongs(l,:),'lat',Slats(l,:));
m_grid('fontsize',6,'xticklabels',[],'xtick',[-180:30:360],...
'ytick',[-80:20:80],'yticklabels',[],'linest','-','color',[.7 .7 .7]);
m_coast('patch','g');
end
xlabel('Interrupted Sinusoidal Projection of World Oceans');
% In order to see all the maps we must undo the axis limits set by m_grid calls:
set(gca,'xlimmode','auto','ylimmode','auto');
subplot(212);
Slongs=[-100 43;-75 20; 20 145;43 100;145 295;100 295];
Slats= [ 0 90;-90 0;-90 0; 0 90;-90 0; 0 90];
for l=1:6
m_proj('mollweide','long',Slongs(l,:),'lat',Slats(l,:));
m_grid('fontsize',6,'xticklabels',[],'xtick',[-180:30:360],...
'ytick',[-80:20:80],'yticklabels',[],'linest','-','color','k')
m_coast('patch',[.6 .6 .6]);
end
xlabel('Interrupted Mollweide Projection of World Oceans');
set(gca,'xlimmode','auto','ylimmode','auto');
5. 斜轴墨卡托投影:矢量场与等值线
%% Nice looking data
[lon,lat]=meshgrid([-136:2:-114],[36:2:54]);
u=sin(lat/6);v=sin(lon/6);
m_proj('oblique','lat',[56 30],'lon',[-132 -120],'aspect',.8);
subplot(121);
m_coast('patch',[.9 .9 .9],'edgecolor','none');
m_grid('tickdir','out','yaxislocation','right',...
'xaxislocation','top','xlabeldir','end','ticklen',.02);
hold on;
m_quiver(lon,lat,u,v);
xlabel('Simulated surface winds');
subplot(122);
m_coast('patch',[.9 .9 .9],'edgecolor','none');m_grid('tickdir','out','yticklabels',[],...
'xticklabels',[],'linestyle','none','ticklen',.02);
hold on;
[cs,h]=m_contour(lon,lat,sqrt(u.*u+v.*v));
clabel(cs,h,'fontsize',8);
xlabel('Simulated something else');
6. 米勒投影与大圆航线
% Plot a circular orbit
lon=[-180:180];
lat=atan(tan(60*pi/180)*cos((lon-30)*pi/180))*180/pi;
m_proj('miller','lat',82);
m_coast('color',[0 .6 0]);
m_line(lon,lat,'linewi',3,'color','r');
m_grid('linestyle','none','box','fancy','tickdir','out');
m_northarrow(-150,0,40,'type',4,'linewi',.5);
7. 西地中海高分辨率水深的兰伯特等角投影
m_proj('lambert','lon',[-10 20],'lat',[33 48]);
[CS,CH]=m_etopo2('contourf',[-5000:500:0 250:250:3000],'edgecolor','none');
m_grid('linestyle','none','tickdir','out','linewidth',3);
colormap([ m_colmap('blues',80); m_colmap('gland',48)]);
brighten(.5);
ax=m_contfbar(1,[.5 .8],CS,CH);
title(ax,{'Level/m',''}); % Move up by inserting a blank line
8. 花式箭头演示
m_vec; % See code in m_vec.m for details
9. 放大爱德华王子岛:不同分辨率的海岸线
% Example showing the default coastline and all of the different resolutions
% of GSHHS coastlines as we zoom in on a section of Prince Edward Island.
clf
axes('position',[.35 .6 .37 .37]);
m_proj('albers equal-area','lat',[40 60],'long',[-90 -50],'rect','on');
m_coast('patch',[0 1 0]);
m_grid('linestyle','none','linewidth',2,'tickdir','out',...
'xaxisloc','top','yaxisloc','right','fontsize',6);
m_text(-69,51,'Standard coastline','color','r','fontweight','bold');
m_ruler([.5 .9],.8,3,'fontsize',8)
axes('position',[.09 .5 .37 .37]);
m_proj('albers equal-area','lat',[40 54],'long',[-80 -55],'rect','on');
m_gshhs_c('patch',[.2 .8 .2]);
m_grid('linestyle','none','linewidth',2,'tickdir','out',...
'xaxisloc','top','fontsize',6);
m_text(-80,52.5,'GSHHS\_C (crude)','color','m','fontweight','bold');
m_ruler([.5 .9],.8,2,'fontsize',8);
axes('position',[.13 .2 .37 .37]);
m_proj('albers equal-area','lat',[43 48],'long',[-67 -58],'rect','on');
m_gshhs_l('patch',[.4 .6 .4]);
m_grid('linestyle','none','linewidth',2,'tickdir','out','fontsize',6);
m_text(-66.5,43.5,'GSHHS\_L (low)','color','m','fontweight','bold');
m_ruler([.5 .9],.8,3,'fontsize',8);
axes('position',[.35 .05 .37 .37]);
m_proj('albers equal-area','lat',[45.8 47.2],'long',[-64.5 -62],'rect','on');
m_gshhs_i('patch',[.5 .6 .5]);
m_grid('linestyle','none','linewidth',2,'tickdir','out',...
'yaxisloc','right','fontsize',6);
m_text(-64.4,45.9,'GSHHS\_I (intermediate) ','color','m',...
'fontweight','bold','horizontalalignment','right');
m_ruler([.5 .8],.1,3,'fontsize',8);
axes('position',[.5 .1 .37 .37]);
m_proj('albers equal-area','lat',[46.375 46.6],'long',[-64.2 -63.7],'rect','on');
m_gshhs_h('patch',[.6 .7 .6]);
m_grid('linestyle','none','linewidth',2,'tickdir','out',...
'xaxisloc','top','yaxisloc','right','fontsize',6);
m_text(-64.18,46.4,'GSHHS\_H (high)','color','m','fontweight','bold');
m_ruler([.5 .8],.2,3,'fontsize',8);
axes('position',[.55 .35 .37 .37]);
m_proj('albers equal-area','lat',[46.55 46.65],'long',[-63.97 -63.77],'rect','on');
m_gshhs_f('patch',[.7 .9 .7]);
m_grid('linestyle','none','linewidth',2,'tickdir','out',...
'xaxisloc','top','yaxisloc','right','fontsize',6);
m_text(-63.95,46.56,'GSHHS\_F (full)','color','m','fontweight','bold');
m_ruler([.5 .8],.2,3,'fontsize',8);
10. 航迹线与 UTM 投影
m_proj('UTM','long',[-72 -68],'lat',[40 44]);
m_gshhs_i('color','k');
m_grid('box','fancy','tickdir','in');
m_ruler(1.2,[.5 .8]);
% fake up a trackline
lons=[-71:.1:-67];
lats=60*cos((lons+115)*pi/180);
dates=datenum(1997,10,23,15,1:41,zeros(1,41));
m_track(lons,lats,dates,'ticks',0,'times',4,'dates',8,...
'clip','off','color','r','orient','upright');
m_northarrow(-68.5,43.4,.4,'type',2);
11. 距离环
m_proj('hammer','clong',170);
m_grid('xtick',[],'ytick',[],'linestyle','-');
m_coast('patch','g');
m_line(100.5,13.5,'marker','square','color','r');
m_range_ring(100.5,13.5,[1000:1000:15000],'color','b','linewi',2);
xlabel('1000km range rings from Bangkok');
12. 点描边界
bndry_lon=[-128.8 -128.8 -128.3 -128 -126.8 -126.6 -128.8];
bndry_lat=[49 50.33 50.33 50 49.5 49 49];
clf;
m_proj('lambert','long',[-130 -121.5],'lat',[47 51.5],'rectbox','on');
m_gshhs_i('color','k'); % Coastline...
m_gshhs_i('speckle','color','k'); % with speckle added
m_line(bndry_lon,bndry_lat,'linewi',2,'color','k'); % Area outline ...
m_hatch(bndry_lon,bndry_lat,'single',30,5,'color','k'); % ...with hatching added.
m_grid('linewi',2,'linest','none','tickdir','out','fontsize',12);
title({'Speckled Boundaries','for nice B&W presentation','(best in postscript format)'});
m_text(-128,48,{'Pacific','Ocean'},'fontsize',18);
m_northarrow(-122.5,50.2,.8,'type',3,'linewi',2);
13. 蓝色海洋
m_proj('miller','lat',[-77 77]);
m_coast('patch',[.7 1 .7],'edgecolor','none');
m_grid('box','fancy','linestyle','-','gridcolor','w','backcolor',[.2 .65 1]);
cities={'Cairo','Washington','Buenos Aires'};
lons=[ 30+2/60 -77-2/60 -58-22/60];
lats=[ 31+21/60 38+53/60 -34-45/60];
for k=1:3
[range,ln,lt]=m_lldist([-123-6/60 lons(k)],[49+13/60 lats(k)],40);
m_line(ln,lt,'color','r','linewi',2);
m_text(ln(end),lt(end),sprintf('%s - %d km',cities{k},round(range)));
end;
title('Great Circle Routes','fontsize',14,'fontweight','bold');
set(gcf,'color','w'); % Need to do this otherwise 'print' turns the lakes black
14. 单一大洋投影
% This projection shows all the oceans connected to each other - the outside ring
% is the Asian coastline (Thanks to M B-O for this idea)
% otherwise its just an example of different map types.
m_proj('azimuthal equal-area','radius',156,'lat',-46,'long',-95,'rot',30);
ax1=subplot(2,2,1,'align');
m_coast('patch','r');
m_grid('xticklabel',[],'yticklabel',[],'linestyle','-','ytick',[-60:30:60]);
ax2=subplot(2,2,2,'align');
m_elev('contourf',[-7000:1000:0 500:500:3000],'edgecolor','none');
colormap(ax2,[m_colmap('blues',70);m_colmap('gland',30)]);
caxis(ax2,[-7000 3000]);
m_grid('xticklabel',[],'yticklabel',[],'linestyle','-','ytick',[-60:30:60]);
ax3=subplot(2,2,3,'align');
colormap(ax3,[m_colmap('blues',70);m_colmap('gland',30)]);
caxis(ax3,[-7000 3000]);
m_elev('image');
m_grid('xticklabel',[],'yticklabel',[],'linestyle','-','ytick',[-60:30:60]);
ax4=subplot(2,2,4,'align');
colormap(ax4,[m_colmap('blues')]);
caxis(ax4,[-8000 000]);
m_elev('shadedrelief','gradient',.5);
m_coast('patch',[.7 .7 .7],'edgecolor','none');
m_grid('xticklabel',[],'yticklabel',[],'linestyle','-','ytick',[-60:30:60]);
ha = axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0 1],'Box','off',...
'Visible','off','Units','normalized', 'clipping' , 'off');
text(0.5, 0.98,'This projection shows all oceans connected to each other',...
'horiz','center','fontsize',20);
15. 阶梯式 jet 配色
% get delta-SA data from the TEOS-10 gsw atlas at 2500 dbar
[LG,LT]=meshgrid(0:360,-86:89);
dSA=ones(size(LG));
dSA(:)=gsw_deltaSA_atlas(3000*dSA(:),LG(:),LT(:));
% Rearrange data to lie in the longitude limits I give for the
% projection
ind=[31:361 1:30]; % Move left side to right
dSA=dSA(:,ind);
LT=LT(:,ind);
LG=LG(:,ind);LG(LG>30)=LG(LG>30)-360; %...and subtract 360 to some longitudes
clf;
m_proj('robinson','lon',[-330 30]);
m_pcolor(LG,LT,dSA*1000);
m_coast('patch',[.7 .7 .7],'edgecolor','none');
m_grid('tickdir','out','linewi',2);
% This is a perceptually uniform jet-like color scale, but in m_colmap
% we can add some simple graduated steps to make the pcolor look a little
% more like a contourf
colormap(m_colmap('jet','step',10));
h=colorbar('northoutside');
title(h,'\deltaSA/(g/kg) at 2000 dbar','fontsize',14);
set(h,'pos',get(h,'pos')+[.2 .05 -.4 0],'tickdir','out')
set(gcf,'color','w'); % Need to do this otherwise 'print' turns the lakes black
16. 水深
load /ocean/rich/home/dens14/VENTS
lp=load('/ocean/rich/home/dens14/Linep_201402');
m_proj('lambert','long',[-160 -115],'lat',[32 60]);
[CS,CH]=m_etopo2('contourf',[-7000:1000:-1000 -500 -200 0 ],'edgecolor','none');
m_gshhs_f('patch',[.7 .7 .7],'edgecolor','none');
h1=m_line(vents.lon,vents.lat,'marker','s','color',[0 .5 0],...
'linest','none','markerfacecolor','w','clip','point');
h2=m_line(lp.POS(:,2),lp.POS(:,1),'marker','o','color','r','linewi',2,...
'linest','none','markersize',8,'markerfacecolor','w');
m_grid('linest','none','tickdir','out','box','fancy','fontsize',16);
legend([h1(1),h2(1)],'Known Hydrothermal vents','Line-P Stations','location','southwest');
colormap(m_colmap('blues'));
caxis([-7000 000]);
[ax,h]=m_contfbar([.55 .75],.8,CS,CH,'endpiece','no','axfrac',.05);
title(ax,'meters')
set(gcf,'color','w'); % otherwise 'print' turns lakes black
17. 晕渲地形(示例 1)
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');
xlabel(ax,'meters','color','k');
18. 晕渲地形(示例 2)
m_proj('utm','ellipse','grs80','zone',10,'lat',[49+15.7/60 49+21/60],...
'long',[-123-15/60 -123-3/60]);
% Uses multibeam bathymetry with 10m horizontal resolution
% Already regularly gridded in UTM coords with vector x2/y2, and
% matrix Z2.
caxis([-150 0]);
colormap([m_colmap('water',128)]);
m_shadedrelief(x2,y2,-Z2,'lightangle',-45,'gradient',8,'coord','z');
% Add some contours
hold on;
[cs,h]=contour(x2,y2,Z2,[0:20:150],'color','k');
clabel(cs,h,'fontsize',6);
hold off;
% Land parts from a previously saved high-resolution coastline
col=[255 214 140]/255; % CHS chart land colour
m_usercoast('/ocean/rich/more/mmapbase/bcgeo/PNW.mat','patch',col);
m_usercoast('/ocean/rich/more/mmapbase/bcgeo/PNWrivers.mat','patch',col);
% Lat/long AND a UTM grid
m_grid('tickdir','out','fontsize',12,'linest','none','xaxisloc','top','yaxisloc','right');
m_utmgrid('xcolor','b','ycolor','b','linest','-');
m_ruler([.5 .8],.9,'tickdir','out','ticklen',[.007 .007]);
m_northarrow(-123-4.5/60,49+19.5/60,1/60,'type',4,'aspect',1.5);
xlabel('Vancouver Harbour','color','k');
19. 风玫瑰图
clf;set(gcf,'color','w');
m_proj('oblique','lon',[-125.3 -122.6],'lat',[50.1 48.6] ,'dir','vert','aspect',0.45)
% Database 1 coastline
m_usercoast('/ocean/rich/more/mmapbase/bcgeo/PNW.mat',...
'patch',[.8 .8 .8],'edgecolor','k');
% Database 2 river
m_usercoast('/ocean/rich/more/mmapbase/bcgeo/PNWrivers.mat',...
'patch',[.8 .8 .8],'edgecolor','k');
% Elevation database in structure SOGbath
hold on; m_contourf(SOGbath.lon,SOGbath.lat,SOGbath.Z,[500 500],...
'edgecolor','none','facecolor',[.7 .7 .7]); hold off;
m_grid('linewi',2,'tickdir','out','ticklen',.03,'linestyl','none','color','k');
m_northarrow(-125.4,49.7,.2,'type',2);
title({'Winter','ONDJFM',' '},'fontsize',14);
% Wind data in struct arrays - pass each parameter to M_WINDROSE as a 1xN
% cell array of data in the order you want them plotted (ones on top later
% in list)
levels=[0:4:24]; % Binning for speeds
k=[1 15 18 6 3 19 4 5 7 8 10 9 11 12 2 13 14 16 17 ]; % change order to
% make overlaps look nicer
m_windrose({Winter(k).lon},{Winter(k).lat},{Winter(k).wdir},{Winter(k).wspd},...
'size',.17,'nspeed',levels,'alpha',1);
colormap(m_colmap('jet'));
caxis([0 24]);
axb=m_contfbar(.8,[.6 .9],levels,levels);
axb.YLabel.String='Wind speeds m/s';
axb.YTick=levels;
20. 配色方案
% This command generates the figure
% In each subplot title is the M_COLMAP call that
% generated the colourmap displayed.
%
% Uses of these colourmaps can be seen
% in other maps in this gallery.
m_colmap demo