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
the cyclist
2023년 8월 12일
@Dyuman Joshi, the plots have equal axes, but they are not ("physically") the same dimension. (See my solution.)
답변 (1개)
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
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Subplots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!