How to have a common colorbar for all tiledlayout plots?

35 次查看(过去 30 天)
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 个评论
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 CenterFile Exchange 中查找有关 Orange 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by