matlab 12子图排版和共用colorbar,横放和竖放
以前发布过。代问的人还是很多。
这次放在一起了。
横放colorbar
竖放colorbar
支持colorbar颜色换:
step 0 1
数据使用来源:
https://www.psl.noaa.gov/data/gridded/data.noaa.oisst.v2.html
读取
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年;
[x,y]=meshgrid(lon,lat);
x=x';y=y';
循环画图:
hh=figure ;
set(gcf,'Position',[100,50,1350,980],'color','w');
m_proj('miller','lon',180,'lat',[-75 75]);
height=0.26;
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
% [c,h]=m_contour(x,y,aim_variable_month,15,'linecolor','white','LineWidth',1);
caxis([0 30])
m_coast('patch',[.85 .85 .85 ],'edgecolor',[0.6 0.6 0.6]);
hold on
end
细节修饰:
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.30,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.345,0.74,0.30,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.652,0.74,0.30,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.30,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.345,0.52,0.30,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.652,0.52,0.30,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.30,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.345,0.3,0.30,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.652,0.3,0.30,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.30,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.345,0.080,0.30,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.652,0.08,0.30,height]);
end
colorbar位置放置:
横:
颜色包包括:颜色包制作可参考:颜色制作
matlab对柱状图的平移保持y刻度不变,给出精细修改和实例
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);
[X,Y]=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')
竖着:
colaxis=[colorbar_text_min,colorbar_text_max];
left=0.960;bottom=0.15;width=0.015;height=0.75;
colpos=[left, bottom, width, height];
%% 建立colorbar位置和颜色
load('D:\matlab_work\十六进制和RGB互转\morecolor6_500.mat');
subplot('position',colpos);
x1=0:1;
y1=colaxis(1):(colaxis(2)-colaxis(1))/1000:colaxis(2);
[X,Y]=meshgrid(x1,y1);
pcolor(X,Y,Y)
shading flat
colormap(hh,morecolor61_500)% 改变填充颜色
set(gca,'XTick',[],'XTickLabel',[' '])
xlabel('°C')
set(gca,'YAxisLocation','right','fontsize',12,'fontweight','bold')
% print(hh,'全球气候态月平均海表温度分布','-dpng','-r600')
颜色包:颜色包制作可参考:颜色制作