Prevent colorbar label overlapping with subplots axis
27 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/342904/image.png)
Larger figure not overlapping
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/342907/image.png)
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 个评论
Sudheer Bhimireddy
2020-8-7
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
回答(1 个)
Peng Li
2020-8-7
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!