MatLab - Creating a plot-in-plot figure

by Ramkrushn Patel

MatLab Plot in Plot

clear; clc
% This script generates a figure with an inset figure - for example a voyage track and sampling arrangement.
% To accomplish this, I'll exploit M_Map package. It should be easy to procure simply by Googling.
% NOTE: If one is unsure about functionality of a function then it's easy to get help in Matlab. Simply by "help functionName" in the command window

% Creating blank figure
figure(3);clf

% Define axis position (left, bottom, width and height)
axes('position',[0.1 0.1 0.85 0.85]);

% setting up projection for the study region
m_proj('lambert','lat',[-51.5, -49.5],'long', [145.5, 148.7])

% Main contour plot
m_contourf(slon, slat, sl, -0.6:0.02:0.6,'linest','none')

% creating colorbar
colormap(redblue(length(-0.6:0.02:0.6) -1)) % set color map of your choice
caxis([-0.6, 0.6]) % color axis limit depends on data range
h = colorbar('position',[0.86, 0.12, 0.02, 0.8],'fontsize',10,'fontweight','bold');
set(h,'ytick',-0.6:0.2:0.6,'yticklabel',-0.6:0.2:0.6) % setting colorbar scale and position
% superimpose multiple plots with different colors, style and markers
hold on
m_plot(nsadcp(1).lon, nsadcp(1).lat, '.', 'linewidth', 1.5, 'color', 'c')
m_plot(nsadcp(2).lon, nsadcp(2).lat, '.', 'linewidth', 1.5, 'color', 'k')
m_plot(nsadcp(3).lon, nsadcp(3).lat, '.', 'linewidth', 1.5, 'color', 'k')
m_plot(nsadcp(4).lon, nsadcp(4).lat, '.', 'linewidth', 1.5, 'color', 'm')
m_plot(nsadcp(5).lon, nsadcp(5).lat, '.', 'linewidth', 1.5, 'color', 'm')
m_plot(nsadcp(6).lon, nsadcp(6).lat, '.', 'linewidth', 1.5, 'color', [0.5, 0.5, 0.5])
m_plot([ctd_ccnf_loc(1:6).lon],[ctd_ccnf_loc(1:6).lat],'sb','MarkerSize',10) % A
m_plot([ctd_ccnf_loc(8:13).lon],[ctd_ccnf_loc(8:13).lat],'>','color',[0.4, 0.4, 0.4],'MarkerSize',8,'MarkerFaceColor',[0.4, 0.4, 0.4]) % B
m_plot([ctd_ccnf_loc(16:21).lon],[ctd_ccnf_loc(16:21).lat],'<m','MarkerSize',8,'MarkerFaceColor','m') % C
m_plot([ctd_ccnf_loc(23).lon],[ctd_ccnf_loc(23).lat],'xb','MarkerSize',10) % Front
m_plot([ctd_ccnf_loc([7, 14:15, 22]).lon],[ctd_ccnf_loc([7, 14:15, 22]).lat],'*b','MarkerSize',8) % centre ops
hold off
 

% Creating coast line
m_coast('patch', [0.9, 0.9, 0.9], 'edgecolor', 'k', 'linewidth',1);

% Creating boundary to define logitude and latitude
m_grid('box', 'fancy', 'fontsize', 12, 'fontweigh', 'bold', 'linest', 'none', 'tickdir', 'in');

 

% Second or inset figure======
axes('position',[0.52 0.18 0.4 0.35]);
m_proj('mercator','lat',[-54, -35],'long', [140, 155])
m_plot(lon, lat, '-','color', [0.5, 0.5, 0.5],'linewidth',1); 
hold on
m_plot([ctd_ccnf_loc([23, 24]).lon],[ctd_ccnf_loc([23, 24]).lat],'*b','MarkerSize',8) % Front profiles
m_line([145.5, 148.7, 148.7, 145.5, 145.5], [-49.5, -49.5, -51.5, -51.5, -49.5],'linest','-','linewidth',1.5,'color','b') %
m_contour(slon, slat, ssh, [0.2   0.2],'-','LineWidth',2,'color',[0,0,0]);
m_contour(slon, slat, ssh, [-0.4 -0.4],'-','LineWidth',2,'color',[0.5,0.5,0.5]);
hold off 
m_coast('patch', [0.8, 0.5, 0.5], 'edgecolor', 'k', 'linewidth',1);
m_grid('box', 'fancy', 'fontsize', 9, 'fontweigh', 'bold', 'linest', 'none', 'tickdir', 'out','xtick',[140, 150],'xticklabels',[140, 150]);
print(gcf,'-dpng','-r300','-painters','Figname1') % to save figure with high resolution. For better zoom capability use '-dpdf' instead '-dpng'

UNSW logo ANU logo Monash logo UMelb logo UTAS logo