Prevent colorbar label overlapping with subplots axis

34 views (last 30 days)
An issue I regularly encounter is the titles for colour bars overlapping with the titles for the y-axis of a neighbouring subplot; and, the title of subplots overlapping with the x-axis neighbouring subplots. I can correct this by changing the size of the whole figure but is there a way of forcing this not to happen?
There image specific things I can do like changing the number of decimal points in the colour bar label or manually moving the position of the labels. But ideally, I would rather have them fixed not to overlap and then change the figure size so I can see the data appropriately.
Overlapping
Larger figure not overlapping
example code I am using
[X, Y] = meshgrid(1:160,1:160);
C = rand(160,160)
label_size = 12
subplot(2,2,1)
surf(X, Y, C)
shading flat
colormap gray
view(0,90)
xlim([0 max(max(160))])
xticks([0, 80, 160])
ylim([0 max(max(160))])
yticks([0, 80, 160])
set(gca,'TickDir','out')
caxis([min(min(C)) max(max(C))])
colorbar
c = colorbar('Ticks',[min(min(C)) max(max(C))]);
titleString = sprintf('Amplitude (mV)');
c.Label.String = titleString;
axis square
set(gca,'FontSize',label_size, 'FontName', 'Times New Roman')
title('(a)','FontSize',label_size)
xlabel('X (mm)','FontSize',label_size)
ylabel('Y (mm)','FontSize',label_size)
and this is repeated for each subplot.
  1 Comment
Sudheer Bhimireddy
Sudheer Bhimireddy on 7 Aug 2020
Alternatively to the answer given below, you could try changing the colorbar range to follow scientific notation. I say this because of your colorbar range. But editing the position of the subplot itself could be more of a general solution.
The below code will draw two subplots side-by-side and by adjusting the gap_x variable you could increase the gap between the subplots.
subplot('Position',[start_x start_y length_x length_y]);
% plot 1
subplot('Position',[(start_x + length_x + gap_x) start_y length_x length_y]);
% plot 2

Sign in to comment.

Answers (1)

Peng Li
Peng Li on 7 Aug 2020
You can specify the Position property of each Axes to the position you'd like it to be at, instead of using subplot. I found this way better as you can vary your whole picture to have different arrangement, with bigger and smaller axes organized.
For example,
figure('Color', 'w', 'Units', 'centimeter', 'Position', [5 5 18 10]);
axes(gcf, 'Units', 'norm', 'Position', [.1 .6 .3 .35]);
% do some plots here
axes(gcf, 'Units', 'norm', 'Position', [.1 .1 .8 .45]);
% another panel
% etc.
also there are OuterPosition and InnerPosition, check the document to have better sense.

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!