How to shape the subplot to square and change colorbar label to top of the bar?

9 views (last 30 days)
I need a square subplots and the colorbar label needs to move to top of the bar. Could some one help me?
set(0,'DefaultAxesFontName','TimesNewRoman');
set(0,'DefaultAxesFontSize',16);
set(0,'DefaultAxesFontWeight','bold')
figure
fig = gcf;
% fig.Position = [0, 0, 10000, 4000];
subplot(2,3,1)
imagesc(randn(10,10))
subplot(2,3,2)
imagesc(randn(10,10))
subplot(2,3,3)
imagesc(randn(10,10))
subplot(2,3,4)
imagesc(randn(10,10))
subplot(2,3,5)
imagesc(randn(10,10))
subplot(2,3,6)
imagesc(randn(10,10))
cbar = colorbar;
cbar_title = 'amp';
cbar.Label.String = cbar_title;
% Adjust the layout to make room for the color bar
set(gcf, 'Position', [100, 100, 1100, 800]);
cbar.Position = [0.92, 0.1, 0.02, 0.8];
  3 Comments
the cyclist
the cyclist on 12 Aug 2023
@Dyuman Joshi, the plots have equal axes, but they are not ("physically") the same dimension. (See my solution.)
Kalasagarreddi Kottakota
Kalasagarreddi Kottakota on 13 Aug 2023
@Dyuman Joshi, I cheked that too, to align my colorbar label. But the code you have given depends on clims. And not working on all the cases.

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 12 Aug 2023
Edited: the cyclist on 12 Aug 2023
One way is to add axis square after each imagesc call:
set(0,'DefaultAxesFontName','TimesNewRoman');
set(0,'DefaultAxesFontSize',16);
set(0,'DefaultAxesFontWeight','bold')
figure
fig = gcf;
% fig.Position = [0, 0, 10000, 4000];
subplot(2,3,1)
imagesc(randn(10,10))
axis square
subplot(2,3,2)
imagesc(randn(10,10))
axis square
subplot(2,3,3)
imagesc(randn(10,10))
axis square
subplot(2,3,4)
imagesc(randn(10,10))
axis square
subplot(2,3,5)
imagesc(randn(10,10))
axis square
subplot(2,3,6)
imagesc(randn(10,10))
axis square
cbar = colorbar;
cbar_title = 'amp';
cbar.Label.String = cbar_title;
% Adjust the layout to make room for the color bar
set(gcf, 'Position', [100, 100, 1100, 800]);
cbar.Position = [0.92, 0.1, 0.02, 0.8];
You might not want to bother with this, because you presumably have a working solution using subplot, but in the future you might also consider using the tiledlayout function to create plots of this type.
  4 Comments
the cyclist
the cyclist on 13 Aug 2023
@Dyuman Joshi, I'm not sure about your perception, but your subplots are rectangular in shape; mine are square.
Take a look at the documentation for the axis command (which governs both axis limits and axis scaling), specifically the two different styles called equal and square.
The default axes are equal, with DataAspectRatio = [1 1]. But they are not square, because their PlotBoxAspectRatio is not [1 1]. The axis square command does that. See the difference side-by-side:
N=7;
tiledlayout(1,2)
nexttile
rng default
scatter(rand(N,1),rand(N,1))
nexttile
rng default
scatter(rand(N,1),rand(N,1))
axis square
the cyclist
the cyclist on 13 Aug 2023
@Kalasagarreddi Kottakota, you can adjust the position of the colorbar label using the Label.Position property. For example in your case
cbar.Label.Position = [2.7 2.0 0]
moves it up near the top.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!