How to have a common colorbar for all tiledlayout plots?

조회 수: 35 (최근 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 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