MatLab - Creating a multiplot figure

by Ramkrushn Patel

 

clear; clc

% Script to plot multiple plot in panels using M_Map package

% NOTE: If one is unsure about functionality of a function then its easy to  get help in Matlab. Simply by typing "help functionName" in command window

% NOTE: It is easy to create everything in a single plot but sometime resolution may not be good due to limitation of print function. To avoid this problem I suggest plotting two separate figures. 1) plot only variables 2) plot only colorbar then using PowerPoint combine and generate plot as shown above in attachment. 

 

% Creating blank figure

figure(1); clf

 

 

% setting up projection for the study region 

%NOTE: if this is common for all the plot then it is okay to use once right after figure command.

m_proj('mercator','lat',[-56, -40],'long', [135, 160])
%=============
% I recommend to use subplot_tight function to create different panels. Because it facilitates control on spaces between figures. Below, I have created three rows, two columns and I'm using 1st position to plot. While in square bracket I mentioned the spaces between two panels in vertical and in horizontal respectively (both in %)

subplot_tight(3,2,1,[0.018, 0.05]) 

 

% Plotting parameters

m_pcolor(lon, lat, sla'); shading interp
caxis([-0.5, 0.5]) % setting colorbar axis
colormap(redblue(length(-0.5:0.02:0.5)-1)) % defining colormap

% adding additional plot using Hold on and Hold off
hold on
m_line([143, 150, 150, 143, 143], [-49 -49 -53, -53, -49],'linest','-','linewidth',2)
m_plot(info(1,4), info(1,5),'*c','linewidth',2)
m_contour(lon, lat, ssh', [0.2   0.2],'-','LineWidth',2,'color',[0,0,0]);
% adding labels in the plot
m_text(135.3, -48.5, 'SAF-N', 'fontsize', 12, 'fontweight', 'bold');
m_contour(lon, lat, ssh', [-0.4 -0.4],'-','LineWidth',2,'color',[0.5, 0.5, 0.5]);
m_text(135.5, -53.0, 'SAF-S', 'fontsize', 12, 'fontweight', 'bold','color',[0.5, 0.5, 0.5]);
str1 =  'Eddy Born';
st1 = '(A) 07 FEB 2016';
m_text(135.4, -44, st1, 'fontsize', 14, 'fontweight', 'bold');
m_text(135.4, -45.5, str1, 'fontsize', 14, 'fontweight', 'bold');
hold off
m_coast('patch',[0.5, 0.8, 0.5],'edgecolor','k','linewidth',1);
m_grid('box','fancy','fontsize',11,'fontweigh','bold','linest','none','tickdir','in','xticklabel',[]);
 

% ==============
subplot_tight(3,2,2,[0.018, 0.05])
[var1, ~, ~] = nc2mat(sla2);
[var2, ~, ~] = nc2mat(adt2);
sla = var1.sla;
lon = var1.lon;
lat = var1.lat;
ssh = var2.adt;
m_pcolor(lon, lat, sla'); shading interp
caxis([-0.5, 0.5])
colormap(redblue(length(-0.5:0.02:0.5)-1))
hold on
m_line([143, 150, 150, 143, 143], [-49 -49 -53, -53, -49],'linest','-','linewidth',2)
m_plot(info(1,4), info(1,5),'*c','linewidth',2)
m_plot(info(1:26,4), info(1:26,5),'-c','linewidth',2)
m_plot(info(26,4), info(26,5),'oc','linewidth',2)
m_contour(lon, lat, ssh', [0.2   0.2],'-','LineWidth',2,'color',[0,0,0]);
m_text(135.3, -48.5, 'SAF-N', 'fontsize', 12, 'fontweight', 'bold');
m_contour(lon, lat, ssh', [-0.4 -0.4],'-','LineWidth',2,'color',[0.5, 0.5, 0.5]);
m_text(135.5, -53.0, 'SAF-S', 'fontsize', 12, 'fontweight', 'bold','color',[0.5, 0.5, 0.5]);
str2 =  'Detached from Meander' ;
st2 = '(B) 03 MAR 2016';
m_text(135.4, -45.5, str2, 'fontsize', 14, 'fontweight', 'bold')
m_text(135.4, -44, st2, 'fontsize', 14, 'fontweight', 'bold');
hold off
m_coast('patch',[0.5, 0.8, 0.5],'edgecolor','k','linewidth',1);
m_grid('box','fancy','fontsize',11,'fontweigh','bold','linest','none','tickdir','in','xticklabel',[],'yticklabel',[]);
 

% NEXT ROW ==================
subplot_tight(3,2,3,[0.018, 0.05])
[var1, ~, ~] = nc2mat(sla3);
[var2, ~, ~] = nc2mat(adt3);
sla = var1.sla;
lon = var1.lon;
lat = var1.lat;
ssh = var2.adt;
m_pcolor(lon, lat, sla'); shading interp
caxis([-0.5, 0.5])
colormap(redblue(length(-0.5:0.02:0.5)-1))
hold on
m_line([143, 150, 150, 143, 143], [-49 -49 -53, -53, -49],'linest','-','linewidth',2)
m_plot(info(1,4), info(1,5),'*c','linewidth',2)
m_plot(info(1:53,4), info(1:53,5),'-c','linewidth',2)
m_plot(info(53,4), info(53,5),'oc','linewidth',2)
m_contour(lon, lat, ssh', [0.2   0.2],'-','LineWidth',2,'color',[0,0,0]);
m_text(135.3, -48.5, 'SAF-N', 'fontsize', 12, 'fontweight', 'bold');
m_contour(lon, lat, ssh', [-0.4 -0.4],'-','LineWidth',2,'color',[0.5, 0.5, 0.5]);
m_text(135.5, -53.0, 'SAF-S', 'fontsize', 12, 'fontweight', 'bold','color',[0.5, 0.5, 0.5]);
str3 =  'Sampling Begin' ;
st3 = '(C) 30 MAR 2016';
m_text(135.4, -44, st3, 'fontsize', 14, 'fontweight', 'bold');
m_text(135.4, -45.5, str3, 'fontsize', 14, 'fontweight', 'bold');
hold off
m_coast('patch',[0.5, 0.8, 0.5],'edgecolor','k','linewidth',1);
m_grid('box','fancy','fontsize',11,'fontweigh','bold','linest','none','tickdir','in','xticklabel',[]);
 

% =================
subplot_tight(3,2,4,[0.018, 0.05])
[var1, ~, ~] = nc2mat(sla4);
[var2, ~, ~] = nc2mat(adt4);
sla = var1.sla;
lon = var1.lon;
lat = var1.lat;
ssh = var2.adt;
m_pcolor(lon, lat, sla'); shading interp
caxis([-0.5, 0.5])
colormap(redblue(length(-0.5:0.02:0.5)-1))
hold on
m_line([143, 150, 150, 143, 143], [-49 -49 -53, -53, -49],'linest','-','linewidth',2)
m_plot(info(1,4), info(1,5),'*c','linewidth',2)
m_plot(info(1:59,4), info(1:59,5),'-c','linewidth',2)
m_plot(info(59,4), info(59,5),'oc','linewidth',2)
m_contour(lon, lat, ssh', [0.2   0.2],'-','LineWidth',2,'color',[0,0,0]);
m_text(135.3, -48.5, 'SAF-N', 'fontsize', 12, 'fontweight', 'bold');
m_contour(lon, lat, ssh', [-0.4 -0.4],'-','LineWidth',2,'color',[0.5, 0.5, 0.5]);
m_text(135.5, -53.0, 'SAF-S', 'fontsize', 12, 'fontweight', 'bold','color',[0.5, 0.5, 0.5]);
str4 = 'Sampling End';
st4 = '(D) 05 APR 2016';
m_text(135.4, -44, st4, 'fontsize', 14, 'fontweight', 'bold');
m_text(135.4, -45.5, str4, 'fontsize', 14, 'fontweight', 'bold');
hold off
m_coast('patch',[0.5, 0.8, 0.5],'edgecolor','k','linewidth',1);
m_grid('box','fancy','fontsize',11,'fontweigh','bold','linest','none','tickdir','in','xticklabel',[],'yticklabel',[]);

% NEXT ROW ====================
subplot_tight(3,2,5,[0.018, 0.05])
[var1, ~, ~] = nc2mat(sla5);
[var2, ~, ~] = nc2mat(adt5);
sla = var1.sla;
lon = var1.lon;
lat = var1.lat;
ssh = var2.adt;
m_pcolor(lon, lat, sla'); shading interp
caxis([-0.5, 0.5])
colormap(redblue(length(-0.5:0.02:0.5)-1))
hold on
m_line([143, 150, 150, 143, 143], [-49 -49 -53, -53, -49],'linest','-','linewidth',2)
m_plot(info(1,4), info(1,5),'*c','linewidth',2)
m_plot(info(1:79,4), info(1:79,5),'-c','linewidth',2)
m_plot(info(79,4), info(79,5),'oc','linewidth',2)
m_contour(lon, lat, ssh', [0.2   0.2],'-','LineWidth',2,'color',[0,0,0]);
m_text(135.3, -48.5, 'SAF-N', 'fontsize', 12, 'fontweight', 'bold');
m_contour(lon, lat, ssh', [-0.4 -0.4],'-','LineWidth',2,'color',[0.5, 0.5, 0.5]);
m_text(135.5, -53.0, 'SAF-S', 'fontsize', 12, 'fontweight', 'bold','color',[0.5, 0.5, 0.5]);
str5 = 'Reattached to Meander';
st5 = '(E) 25 APR 2016';
m_text(135.4, -44, st5, 'fontsize', 14, 'fontweight', 'bold');
m_text(135.4, -45.5, str5, 'fontsize', 14, 'fontweight', 'bold');
hold off
m_coast('patch',[0.5, 0.8, 0.5],'edgecolor','k','linewidth',1);
m_grid('box','fancy','fontsize',11,'fontweigh','bold','linest','none','tickdir','in');
 

%=========================
subplot_tight(3,2,6,[0.018, 0.05])
[var1, ~, ~] = nc2mat(sla6);
[var2, ~, ~] = nc2mat(adt6);
sla = var1.sla;
lon = var1.lon;
lat = var1.lat;
ssh = var2.adt;
m_pcolor(lon, lat, sla'); shading interp
caxis([-0.5, 0.5])
colormap(redblue(length(-0.5:0.02:0.5)-1))
hold on
m_line([143, 150, 150, 143, 143], [-49 -49 -53, -53, -49],'linest','-','linewidth',2)
m_plot(info(1,4), info(1,5),'*c','linewidth',2)
m_plot(info(1:108,4), info(1:108,5),'-c','linewidth',2)
m_plot(info(108,4), info(108,5),'oc','linewidth',2)
m_contour(lon, lat, ssh', [0.2   0.2],'-','LineWidth',2,'color',[0,0,0]);
m_text(135.3, -48.5, 'SAF-N', 'fontsize', 12, 'fontweight', 'bold');
m_contour(lon, lat, ssh', [-0.4 -0.4],'-','LineWidth',2,'color',[0.5, 0.5, 0.5]);
m_text(135.5, -53.0, 'SAF-S', 'fontsize', 12, 'fontweight', 'bold','color',[0.5, 0.5, 0.5]);
str6 = 'Eddy Last Seen';
st6 = '(F) 25 MAY 2016';
m_text(135.4, -44, st6, 'fontsize', 14, 'fontweight', 'bold');
m_text(135.4, -45.5, str6, 'fontsize', 14, 'fontweight', 'bold');
hold off
m_coast('patch',[0.5, 0.8, 0.5],'edgecolor','k','linewidth',1);
m_grid('box','fancy','fontsize',11,'fontweigh','bold','linest','none','tickdir','in','yticklabel',[]);
%
print(gcf,'-dpng','-r300','-painters','Figure1')

%% Only colorbar that I appended underneath the above figure.
close all
figure(2);clf
m_proj('mercator','lat',[-56, -40],'long', [135, 160])
subplot_tight(3,2,3,[0.018, 0.05])
m_pcolor(lon, lat, sla'); shading interp
caxis([-0.5, 0.5])
colormap(redblue(length(-0.5:0.02:0.5)-1))
colorbar('horizontal','position',[0.1 0.08 0.7 0.03],'fontsize',11)
hold on
m_plot(info(1,4), info(1,5),'*c','linewidth',2)
m_plot(info(1:53,4), info(1:53,5),'-c','linewidth',2)
m_plot(info(53,4), info(53,5),'oc','linewidth',2)
m_contour(lon, lat, ssh', [0.2   0.2],'-','LineWidth',2,'color',[0,0,0]);
m_text(135.3, -48.5, 'SAF-N', 'fontsize', 12, 'fontweight', 'bold');
m_contour(lon, lat, ssh', [-0.4 -0.4],'-','LineWidth',2,'color',[0.5,0.5,0.5]);
m_text(135.5, -53.0, 'SAF-S', 'fontsize', 12, 'fontweight', 'bold','color',[0.5,0.5,0.5]);
m_contour(lon, lat, ssh', [-1.0 -1.0],'-','LineWidth',2,'color',[0,0,0]);
m_text(146.0, -59.3, 'PF', 'fontsize', 12, 'fontweight', 'bold');
m_text(135.4, -45.5, '30 MAR 2016', 'fontsize', 12, 'fontweight', 'bold');
hold off
m_coast('patch',[0.9, 0.9, 0.9],'edgecolor','k','linewidth',1);
m_grid('box','fancy','fontsize',11,'fontweigh','bold','linest','none','tickdir','in','xticklabel',[]);
print(gcf,'-dpng','-r300','-painters','figure11')

UNSW logo ANU logo Monash logo UMelb logo UTAS logo