Colorbar String Spacing Issue
Show older comments
I have some issues using 'colorbar'. To my problem: I would like to to plot a certain number (let's say 4) of different contourf-diagrams in a created 'tiledlayout'. The content is not important for my issue which is why I simply chose 'peaks' in the following. To keep the whole thing concise, a common x- and y-label should be used.
fig= figure('color', 'w');
tile= tiledlayout(2,2,'TileSpacing','normal','Padding', 'normal');
for a=1:4
nexttile(a);
contourf(peaks);
set(gca, 'FontSize', 14);
title(['Plot No. ', num2str(a)],'FontName','Arial Narrow','FontSize',14);
end
xlabel(tile, 'X-Label [unit]');
ylabel(tile, 'Y-Label [unit]');
For all plots I would like to place a common central colorbar below. However, this colorbar should still be provided with a label. I first tried to insert a string like in the examples:
cb= colorbar('FontName','Arial Narrow','FontSize',14);
% attempt #1:
cb.Label.String = 'CBar Label';
cb.AxisLocation = 'in';
cb.Layout.Tile= 'south'; % 'in' and 'out' doesn't make a difference in the south location

The problem is that the string of the colorbar is colliding with the xlabel. First I tought it just has to be done in diferent order (without success) :
% cb.Layout.Tile= 'south';
% cb.Label.String = 'CBar Label';
% cb.AxisLocation = 'in';
When I completely leave out the xlabel, the string will be truncated and is only partially displayed. Another idea was to use the title-function:
% attempt #2:
title(cb,'CBar Label', 'FontSize',14); % alternatively tried: xlabel(cb,...) and ylabel(cb,...)
cb.Layout.Tile= 'south';

Here the x-label is placed under the color bar, which is very unfavorable and misleading. I know that 'tiledlayout' is a relatively new and still under development but this problem occurs when using single plots as well. It seems to me that there is a problem with the location, as if the label is not correctly linked to the colorbar. So maybe the reason is wrong figure properties. I hope somebody can help. Thanks in advance!
3 Comments
Adam Danz
on 17 May 2021
What release of Matlab are you using? I cannot replicate the problem in r2021a.
Accepted Answer
More Answers (0)
Categories
Find more on Colorbar 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!