How to have a common colorbar for all tiledlayout plots?

34 visualizaciones (últimos 30 días)
Abhishek Chakraborty
Abhishek Chakraborty el 17 de Jun. de 2022
Comentada: Voss el 21 de Nov. de 2022
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?

Respuesta aceptada

Voss
Voss el 17 de Jun. de 2022
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 comentarios
Ranu Ghafour
Ranu Ghafour el 21 de Nov. de 2022
@Voss is there a way that I can decrease the length of colorbar?
Voss
Voss el 21 de Nov. de 2022
Depending on what you want, you might try playing with the colorbar's Position or the axes' CLim.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Orange en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by