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

21 次查看(过去 30 天)
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 个评论
Kalasagarreddi Kottakota
@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.

请先登录,再进行评论。

回答(1 个)

the cyclist
the cyclist 2023-8-12
编辑:the cyclist 2023-8-12
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 个评论
the cyclist
the cyclist 2023-8-13
@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 2023-8-13
@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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by