matlab全球气候态月平均海表温度处理过程
读取数据:
ncdisp 'sst.mnmean.nc'% 查看变量方便读取
file_sst='sst.mnmean.nc';
lat=double(ncread(file_sst,'lat'));
lon=double(ncread(file_sst,'lon'));
sst=double(ncread(file_sst,'sst'));
sst=sst(:,:,1:480);%取40年;
[x,y]=meshgrid(lon,lat);
x=x';y=y';
求平均:原理,把所有相同的月份相加求平均。直接使用mean即可。
for month_i=1:12
aim_variable_month(:,:,month_i)=squeeze(nanmean(sst(:,:,month_i:12:end),3));% 求月平均
end
画图:
clear;clc;close all
% 想12个月的温度数据,
%画出12个子图
% 12个子图共用1个colorbar
ncdisp 'sst.mnmean.nc'% 查看变量方便读取
file_sst='sst.mnmean.nc';
lat=double(ncread(file_sst,'lat'));
lon=double(ncread(file_sst,'lon'));
sst=double(ncread(file_sst,'sst'));
sst=sst(:,:,1:480);%取40年;
[ ]=meshgrid(lon,lat);
x=x';y=y';
%%
%-----plot-----start
colorbar_text_min=0;
colorbar_text_max=30;
hh=figure ;
set(gcf,'Position',[100,50,1250,980],'color','w');
m_proj('miller','lon',180,'lat',[-75 75]);
height=0.265;
for month_i=1:12
aim_variable_month=squeeze(nanmean(sst(:,:,month_i:12:end),3));% 求月平均
hhh=subplot(4,3,month_i);
m_contourf(x,y,aim_variable_month,200,'linestyle','none');
hold on
[15,'linecolor','white','LineWidth',1); ]=m_contour(x,y,aim_variable_month,
caxis([0 30])
m_coast('patch',[.85 .85 .85 ],'edgecolor',[0.6 0.6 0.6]);
hold on
if month_i==1
m_grid('linest','-.','xtick',7,'xticklabel',[],'ytick',6,'tickdir','in','FontName','Times new roman','FontSize',15,'color','k','box','fancy','fontweight','bold');
set(hhh,'position',[0.04,0.74,0.305,height]);
set(gca,'xticklabel',[],'fontsize',12);
elseif month_i==2
m_grid('linest','-.','xtick',7,'xticklabel',[],'ytick',6,'yticklabel',[],'tickdir','in','FontName','Times new roman','FontSize',15,'color','k','box','fancy','fontweight','bold');
set(hhh,'position',[0.35,0.74,0.305,height]);
set(gca,'xticklabel',[],'yticklabel',[]);
elseif month_i==3
m_grid('linest','-.','xtick',7,'xticklabel',[],'ytick',6,'yticklabel',[],'tickdir','in','FontName','Times new roman','FontSize',15,'color','k','box','fancy','fontweight','bold');
set(gca,'xticklabel',[],'yticklabel',[]);
set(hhh,'position',[0.66,0.74,0.305,height]);
elseif month_i==4
m_grid('linest','-.','xtick',7,'xticklabel',[],'ytick',6,'tickdir','in','FontName','Times new roman','FontSize',15,'color','k','box','fancy','fontweight','bold');
set(hhh,'position',[0.04,0.52,0.305,height]);
set(gca,'xticklabel',[],'fontsize',12);
elseif month_i==5
m_grid('linest','-.','xtick',7,'xticklabel',[],'ytick',6,'yticklabel',[],'tickdir','in','FontName','Times new roman','FontSize',15,'color','k','box','fancy','fontweight','bold');
set(gca,'xticklabel',[],'yticklabel',[]);
set(hhh,'position',[0.35,0.52,0.305,height]);
elseif month_i==6
m_grid('linest','-.','xtick',7,'xticklabel',[],'ytick',6,'yticklabel',[],'tickdir','in','FontName','Times new roman','FontSize',15,'color','k','box','fancy','fontweight','bold');
set(gca,'xticklabel',[],'yticklabel',[]);
set(hhh,'position',[0.66,0.52,0.305,height]);
elseif month_i==7
m_grid('linest','-.','xtick',7,'xticklabel',[],'ytick',6,'tickdir','in','FontName','Times new roman','FontSize',15,'color','k','box','fancy','fontweight','bold');
set(gca,'xticklabel',[]);
set(hhh,'position',[0.04,0.3,0.305,height]);
elseif month_i==8
m_grid('linest','-.','xtick',7,'xticklabel',[],'ytick',6,'yticklabel',[],'tickdir','in','FontName','Times new roman','FontSize',15,'color','k','box','fancy','fontweight','bold');
set(gca,'xtick',[],'xticklabel',[],'ytick',[],'yticklabel',[]);
set(hhh,'position',[0.35,0.3,0.305,height]);
elseif month_i==9
m_grid('linest','-.','xtick',7,'xticklabel',[],'ytick',6,'yticklabel',[],'tickdir','in','FontName','Times new roman','FontSize',15,'color','k','box','fancy','fontweight','bold');
set(gca,'xtick',[],'xticklabel',[],'ytick',[],'yticklabel',[]);
set(hhh,'position',[0.66,0.3,0.305,height]);
elseif month_i==10
m_grid('linest','-.','xtick',7,'ytick',6,'tickdir','in','FontName','Times new roman','FontSize',15,'color','k','box','fancy','fontweight','bold');
set(hhh,'position',[0.04,0.080,0.305,height]);
elseif month_i==11
m_grid('linest','-.','xtick',7,'ytick',6,'yticklabel',[],'tickdir','in','FontName','Times new roman','FontSize',15,'color','k','box','fancy','fontweight','bold');
set(gca,'ytick',[],'yticklabel',[]);
set(hhh,'position',[0.35,0.080,0.305,height]);
elseif month_i==12
m_grid('linest','-.','xtick',7,'ytick',6,'yticklabel',[],'tickdir','in','FontName','Times new roman','FontSize',15,'color','k','box','fancy','fontweight','bold');
set(gca,'ytick',[],'yticklabel',[]);
set(hhh,'position',[0.66,0.08,0.305,height]);
end
hold on
m_text(60,50,[num2str(month_i),'月'] ,'fontsize',16,'color','k','fontweight','bold')
end
colaxis=[colorbar_text_min,colorbar_text_max];
left=0.1;bottom=0.035;width=0.8;height=0.03;
colpos=[left, bottom, width, height];
%% 建立colorbar位置和颜色
load('D:\matlab_work\十六进制和RGB互转\morecolor1_500.mat');
subplot('position',colpos);
x1=0:1;
y1=colaxis(1):(colaxis(2)-colaxis(1))/1000:colaxis(2);
[ ]=meshgrid(x1,y1);
pcolor(Y,X,Y)
shading flat
colormap(hh,morecolor11_500)% 改变填充颜色
set(gca,'YTick',[],'YTickLabel',[' '])
ylabel('°C')
set(gca,'XAxisLocation','bottom','fontsize',12,'fontweight','bold')
% print(hh,'全球气候态月平均海表温度分布','-dpng','-r600')
export_fig('全球气候态月平均海表温度分布1.jpg','-r300')