How to make one colorbar for a 2x2 subplot

25 vues (au cours des 30 derniers jours)
Shan  Chu
Shan Chu le 13 Fév 2020
Hi all,
I would like to insert one colorbar into a 2x2 subplot. I have read the answer in this thread https://uk.mathworks.com/matlabcentral/answers/144453-how-to-make-one-colorbar-for-all-subplots
However, I couldn't see the ticks in the colorbar as well as the axis title for the colorbar.
Could you please help?
Thanks
figure
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)

Réponses (1)

fred  ssemwogerere
fred ssemwogerere le 13 Fév 2020
Hello, to see the tick marks on the colorbar with their corresponding numerical labels, you could do it manually from the figure window, by selecting the the last icon ("Show Plot Tools and Dock Figure") on the menu bar, click the colorbar and then make manual adjustment to its extent from the corners. The axis label can be incorporated (based on your code), with:
h.Label.String='myaxis'; % you can edit the string 'myaxis'
  2 commentaires
fred  ssemwogerere
fred ssemwogerere le 13 Fév 2020
The new position of the colorbar after manual adjustment in the figure window, can be obtained from the command:
h.Position
fred  ssemwogerere
fred ssemwogerere le 13 Fév 2020
Alternatively, to see the tick marks, you could modify the inputs to the position with code:
h=colorbar('Position', [hp4(1)+hp4(3)+0.01 hp4(2) 0.0455 hp4(2)+hp4(3)*2.1]);

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by