簡單易學(xué),教你如何用m_map繪制好看的SSS與SSH動圖
前景提要:這次的專欄分享將給大家?guī)砣绾问褂胢atlab的m_map繪制出好看的SSH與SSS圖片。本文仍舊建議PC端閱讀(這原本是我的一個作業(yè),費了不少心思,結(jié)果交作業(yè)的時候發(fā)現(xiàn)時間分辨率不對,不符合作業(yè)要求,血壓直線飆升。)
?

成果展示:
?

(gif圖上傳太慢了,SSS就算了)
準(zhǔn)備工作:裝有m_map的matlab

數(shù)據(jù)選取:NCEP Global Ocean Data Assimilation System (GODAS)的2011-2021年SSS與SSH的月平均數(shù)據(jù),(小聲bb,挖個坑,什么時候介紹一下這個,個人感覺挺好用的)
?

?

代碼展示:以SSS為例
代碼一:生成gif的每一幀
????clc;clear;clf
????boundary = [115 250 0 65];
????num=1;
????for year=2011:2021
????????source=['salt.' num2str(year) '.nc'];
????????info = ncinfo(source);
????????data=info.Variables;
????????lat=ncread(source,'lat');
????????lon=ncread(source,'lon');
?????????
????????%查找繪制范圍對應(yīng)的所在矩陣的位置
????????lon_scope = find(lon >= boundary(1) & lon<=boundary(2));
????????lat_scope = find(lat >= boundary(3) & lat<=boundary(4));
????????%繪制范圍的緯度
????????Dlon=lon(lon_scope);
????????Dlat=lat(lat_scope);
?????????
????????salinity = ncread(source,'salt');
?????????
????????for t=1:12
????????????salinity_plot=salinity(lon_scope(1):lon_scope(end),lat_scope(1):lat_scope(end),1,t)';
????????????location=salinity_plot<0;
????????????salinity_plot(location)=nan;
????????????clear location
?????????????
????????????%蘭伯特投影,因為我繪制的是北太平洋,有高緯度地區(qū)。大家可以根據(jù)實際情況改
????????????m_proj('lambert','lat',[boundary(3) boundary(4)],'lon',[boundary(1) boundary(2)]);
?????????????
????????????hold on
????????????%繪制圖形
????????????m_pcolor(Dlon,Dlat,salinity_plot) %添加我們要畫的內(nèi)容
????????????m_coast('color',[0 0 0],'linewidth',2); %繪制海岸線,填充陸地
????????????shading interp
????????????m_grid('box','fancy') %添加邊框
????????????hold on
????????????%添加標(biāo)題
????????????title(['2011-2021 Mouthly Sea Surface Salinity_' num2str(year) '_' num2str(t)],'fontsize',15,Interpreter='none')?
????????????%添加色標(biāo)
????????????h = colorbar('h');
????????????caxis([0.031 0.036])
????????????set(get(h,'title'),'string','kg');
????????????saveas(gcf,[num2str(num) '.tif']);
????????????num=num+1;
????????????%保存為tif圖片
????????????clear salinity_plot
????????end
????end
?
代碼二:生成gif
????clc ; clear ;close all;
????filename = 'SSS';
????for t = 1:132%這里應(yīng)該到上文代碼生成的文件數(shù)量
????????str = strcat(num2str(t),'.tif');
????????K = imread(str);
????????[A,map] = rgb2ind(K,256);
????????if t == 1
????????????imwrite(A,map,filename,'gif','LoopCount',inf,'DelayTime',0.1);
????????else
????????????imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.1);
????????end
????end
?

寫在最后:有一說一,我的這段代碼是我自身去學(xué)習(xí)其他人的代碼然后根據(jù)自己的數(shù)據(jù)進行修改改進然后繪制而成的,但也因此不可避免地存在一些其他人代碼的影子。如果您感覺我抄襲了您的代碼,請及時聯(lián)系我,我會第一時間進行修改,甚至刪除這篇文章。
?