Colorbar String Spacing Issue

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

What release of Matlab are you using? I cannot replicate the problem in r2021a.
Sebastian
Sebastian on 17 May 2021
Edited: Sebastian on 17 May 2021
Okay, we are using Matlab 2020b.
Adam Danz
Adam Danz on 17 May 2021
Edited: Adam Danz on 20 May 2021
Perhaps you could try the ylabel() method shown in my answer. It might position the label differently than assing the string directly to the Label property.

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 17 May 2021
Edited: Adam Danz on 17 May 2021
The problem with overlapping labels must have been solved in Matlab R2021a.
I agree that the xlabel should be above the colorbar. You could submit a feature request: Contact Us - MATLAB & Simulink
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]');
cb= colorbar('FontName','Arial Narrow','FontSize',14);
cb.Layout.Tile= 'south';
ylabel(cb,'CBar Label');
title(cb, 'CBar title')

More Answers (0)

Asked:

on 17 May 2021

Edited:

on 20 May 2021

Community Treasure Hunt

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

Start Hunting!