MATLAB: How to make one Colorbar for a 2×2 subplot

colorbarsubplot

Hi everyone,
I would like to make one Colorbar for a 2×2 subplot on right hand side. I have read the answer of a similar question: https://uk.mathworks.com/matlabcentral/answers/144453-how-to-make-one-colorbar-for-all-subplots
But I couldn't see the tick of the colorbar as well as the title. Could you please help?
Thanks
Here is my code:
subplot(2,2,1)
[C,h]=contour(f_mat,sig_mat,real(L_lossy_mat).'*1e9,'LineWidth',2);
h.LevelList = [-1 -0.5 -0.2 -0.05 0.0 0.03 0.04 0.05 0.06];
clabel(C,h,'LabelSpacing',100,'FontSize',14,'FontWeight','bold','FontSmoothing','on','Interpreter','Latex')
ylabel('$\sigma^{(\textbf{I})}$ (S/m)','Interpreter','Latex')
xlabel('$f$ (MHz)','Interpreter','Latex')
title('$R=\infty$','Interpreter','Latex','Color',[125/255 30/255 125/255])
ax=gca;
ax.YDir = 'reverse';
ax.YScale = 'log';
ax.YMinorGrid = 'off';
ax.TickLabelInterpreter = 'latex';
xticks([37 46 55])
yticks([0.01 0.1 1 10])
yticklabels({'0.01','0.1','1','10'})
subplot(2,2,2)
[C,h]=contour(f_cst,sig_cst,real(L_11_cst).'*1e9,'LineWidth',2);
h.LevelList = [-1 -0.5 -0.2 -0.05 0.0 0.03 0.04 0.05 0.06];
clabel(C,h,'LabelSpacing',100,'FontSize',14,'FontWeight','bold','FontSmoothing','on','Interpreter','Latex')
ylabel('$\sigma^{(\textbf{I})}$ (S/m)','Interpreter','Latex')
xlabel('$f$ (MHz)','Interpreter','Latex')
title('$R=1.1r_0$','Interpreter','Latex','Color',[125/255 30/255 125/255])
ax=gca;
ax.YDir = 'reverse';
ax.YScale = 'log';
ax.YMinorGrid = 'off';
ax.TickLabelInterpreter = 'latex';
xticks([37 46 55])
yticks([0.01 0.1 1 10])
yticklabels({'0.01','0.1','1','10'})
subplot(2,2,3)
[C,h]=contour(f_cst,sig_cst,real(L_20_cst).'*1e9,'LineWidth',2);
h.LevelList = [-1 -0.5 -0.2 -0.05 0.0 0.03 0.04 0.05 0.06];
clabel(C,h,'LabelSpacing',100,'FontSize',14,'FontWeight','bold','FontSmoothing','on','Interpreter','Latex')
ylabel('$\sigma^{(\textbf{I})}$ (S/m)','Interpreter','Latex')
xlabel('$f$ (MHz)','Interpreter','Latex')
title('$R=2r_0$','Interpreter','Latex','Color',[125/255 30/255 125/255])
ax=gca;
ax.YDir = 'reverse';
ax.YScale = 'log';
ax.YMinorGrid = 'off';
ax.TickLabelInterpreter = 'latex';
xticks([37 46 55])
yticks([0.01 0.1 1 10])
yticklabels({'0.01','0.1','1','10'})
subplot(2,2,4)
[C,h]=contour(f_cst,sig_cst,real(L_30_cst).'*1e9,'LineWidth',2);
h.LevelList = [-1 -0.5 -0.2 -0.05 0.0 0.03 0.04 0.05 0.06 0.08];
clabel(C,h,'LabelSpacing',200,'FontSize',14,'FontWeight','bold','FontSmoothing','on','Interpreter','Latex')
ylabel('$\sigma^{(\textbf{I})}$ (S/m)','Interpreter','Latex')
xlabel('$f$ (MHz)','Interpreter','Latex')
title('$R=3r_0$','Interpreter','Latex','Color',[125/255 30/255 125/255])
ax=gca;
ax.YDir = 'reverse';
ax.YScale = 'log';
ax.YMinorGrid = 'off';
ax.TickLabelInterpreter = 'latex';
xticks([37 46 55])
yticks([0.01 0.1 1 10])
yticklabels({'0.01','0.1','1','10'})
colormap(hsv(1000))
hp4 = get(subplot(2,2,4),'Position')
h = colorbar('Position', [hp4(1)+hp4(3)+0.02 hp4(2) 0.1 hp4(2)+hp4(3)*2.1]);
h.TickLabelInterpreter = 'latex';
caxis([-1.8 0.1])
ylabel(h, '$\rm{Re}\{\, \mathcal{L} \,\}$ (nH)','Interpreter','Latex')
set(gcf,'Color','w')
set(findall(gcf,'-property','FontName'),'fontsize',18)

Best Answer

The problem is with the location and size of the colorbar. By changing the Position of the colobar to the left i.e., reduce the left value of the position: [left bottom width height] you can see the complete colorbar but on the top of subplots 2 & 4.
For this particular problem I would suggest you to make a subplot grid of 2x3 instead of 2x2 so that you can make use of the space of 3 & 6 subplots and change the Position property of the colorbar accordingly. The following code will help you
figure
subplot(2,3,1)
[C,h]=contour(f_mat,sig_mat,real(L_lossy_mat).'*1e9,'LineWidth',2);
h.LevelList = [-1 -0.5 -0.2 -0.05 0.0 0.03 0.04 0.05 0.06];
clabel(C,h,'LabelSpacing',100,'FontSize',14,'FontWeight','bold','FontSmoothing','on','Interpreter','Latex')
ylabel('$\sigma^{(\textbf{I})}$ (S/m)','Interpreter','Latex')
xlabel('$f$ (MHz)','Interpreter','Latex')
title('$R=\infty$','Interpreter','Latex','Color',[125/255 30/255 125/255])
ax=gca;
ax.YDir = 'reverse';
ax.YScale = 'log';
ax.YMinorGrid = 'off';
ax.TickLabelInterpreter = 'latex';
xticks([37 46 55])
yticks([0.01 0.1 1 10])
yticklabels({'0.01','0.1','1','10'})
subplot(2,3,2)
[C,h]=contour(f_cst,sig_cst,real(L_11_cst).'*1e9,'LineWidth',2);
h.LevelList = [-1 -0.5 -0.2 -0.05 0.0 0.03 0.04 0.05 0.06];
clabel(C,h,'LabelSpacing',100,'FontSize',14,'FontWeight','bold','FontSmoothing','on','Interpreter','Latex')
ylabel('$\sigma^{(\textbf{I})}$ (S/m)','Interpreter','Latex')
xlabel('$f$ (MHz)','Interpreter','Latex')
title('$R=1.1r_0$','Interpreter','Latex','Color',[125/255 30/255 125/255])
ax=gca;
ax.YDir = 'reverse';
ax.YScale = 'log';
ax.YMinorGrid = 'off';
ax.TickLabelInterpreter = 'latex';
xticks([37 46 55])
yticks([0.01 0.1 1 10])
yticklabels({'0.01','0.1','1','10'})
subplot(2,3,4)
[C,h]=contour(f_cst,sig_cst,real(L_20_cst).'*1e9,'LineWidth',2);
h.LevelList = [-1 -0.5 -0.2 -0.05 0.0 0.03 0.04 0.05 0.06];
clabel(C,h,'LabelSpacing',100,'FontSize',14,'FontWeight','bold','FontSmoothing','on','Interpreter','Latex')
ylabel('$\sigma^{(\textbf{I})}$ (S/m)','Interpreter','Latex')
xlabel('$f$ (MHz)','Interpreter','Latex')
title('$R=2r_0$','Interpreter','Latex','Color',[125/255 30/255 125/255])
ax=gca;
ax.YDir = 'reverse';
ax.YScale = 'log';
ax.YMinorGrid = 'off';
ax.TickLabelInterpreter = 'latex';
xticks([37 46 55])
yticks([0.01 0.1 1 10])
yticklabels({'0.01','0.1','1','10'})
subplot(2,3,5)
[C,h]=contour(f_cst,sig_cst,real(L_30_cst).'*1e9,'LineWidth',2);
h.LevelList = [-1 -0.5 -0.2 -0.05 0.0 0.03 0.04 0.05 0.06 0.08];
clabel(C,h,'LabelSpacing',200,'FontSize',14,'FontWeight','bold','FontSmoothing','on','Interpreter','Latex')
ylabel('$\sigma^{(\textbf{I})}$ (S/m)','Interpreter','Latex')
xlabel('$f$ (MHz)','Interpreter','Latex')
title('$R=3r_0$','Interpreter','Latex','Color',[125/255 30/255 125/255])
ax=gca;
ax.YDir = 'reverse';
ax.YScale = 'log';
ax.YMinorGrid = 'off';
ax.TickLabelInterpreter = 'latex';
xticks([37 46 55])
yticks([0.01 0.1 1 10])
yticklabels({'0.01','0.1','1','10'})
colormap(hsv(1000))
hp4 = get(subplot(2,3,5),'Position');
h = colorbar('Position', [hp4(1)+hp4(3)+0.1 hp4(2) 0.1 hp4(4)*2.2]);
h.TickLabelInterpreter = 'latex';
caxis([-1.8 0.1])
ylabel(h, '$\rm{Re}\{\, \mathcal{L} \,\}$ (nH)','Interpreter','Latex')
set(gcf,'Color','w')
set(findall(gcf,'-property','FontName'),'fontsize',18)
Related Question