How to have a common colorbar for all tiledlayout plots?

38 ビュー (過去 30 日間)
Abhishek Chakraborty
Abhishek Chakraborty 2022 年 6 月 17 日
コメント済み: Voss 2022 年 11 月 21 日
I want to plot two arrays A and B (both having 4 rows and 8 columns) using the tiledlayout format so that both of them have a common colorbar. I tried the following code:
A=[14,8,3,5,6,5,3,3;16,2,7,-7,3,1,1,4;3,1,1,1,1,0,0,0;3,1,2,1,1,0,0,0];
B=[5,4,3,3,1,0,0,0;33,0,5,8,5,4,3,5;0,0,0,1,0,0,0,0;2,0,1,2,0,0,0,0];
figure;
t = tiledlayout(1,2,'TileSpacing','compact','Padding','compact');
nexttile;
imagesc(A);
title('A','FontWeight','bold','FontSize',12,'FontName','Helvetica');
nexttile;
imagesc(B);
title('B','FontWeight','bold','FontSize',12,'FontName','Helvetica');
cb = colorbar;cb.Layout.Tile = 'east';
Notice that the element at row=2 and column=4 of array 'A' is a negative number (-7). However, the common colorbar made by the code given above is only showing positive values as shown in the screenshot given below:
How to solve this issue so that the common colorbar shows all the range of values in both the tiles?

採用された回答

Voss
Voss 2022 年 6 月 17 日
You can calculate the min and max value of A and B together and set the CLim of both axes to those values using the clim (formerly known as caxis) function.
A=[14,8,3,5,6,5,3,3;16,2,7,-7,3,1,1,4;3,1,1,1,1,0,0,0;3,1,2,1,1,0,0,0];
B=[5,4,3,3,1,0,0,0;33,0,5,8,5,4,3,5;0,0,0,1,0,0,0,0;2,0,1,2,0,0,0,0];
c_min = min([A(:); B(:)]);
c_max = max([A(:); B(:)]);
figure;
t = tiledlayout(1,2,'TileSpacing','compact','Padding','compact');
nexttile;
imagesc(A);
clim([c_min c_max]);
title('A','FontWeight','bold','FontSize',12,'FontName','Helvetica');
nexttile;
imagesc(B);
clim([c_min c_max]);
title('B','FontWeight','bold','FontSize',12,'FontName','Helvetica');
cb = colorbar;
cb.Layout.Tile = 'east';
  4 件のコメント
Ranu Ghafour
Ranu Ghafour 2022 年 11 月 21 日
@Voss is there a way that I can decrease the length of colorbar?
Voss
Voss 2022 年 11 月 21 日
Depending on what you want, you might try playing with the colorbar's Position or the axes' CLim.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrange についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by