How to set properly single colorbar for subplots?
112 views (last 30 days)
Show older comments
Kalasagarreddi Kottakota
on 8 Aug 2023
Edited: Florian Bidaud
on 8 Aug 2023
Hi, As you see here, I need only one globar color bar for all the subplots. But when I try using this, the first two subplots are of one size and last one is of different size. How to resolve this issue?
% Sample data for the subplots
data1 = rand(10, 10);
data2 = rand(10, 10);
data3 = rand(10, 10);
% Set the default font size for all subplots in the figure
set(0, 'DefaultAxesFontSize', 20);
% Create a figure
figure;
% Create the first subplot
subplot(1, 3, 1);
imagesc(data1);
title('Subplot 1');
% Create the second subplot
subplot(1, 3, 2);
imagesc(data2);
title('Subplot 2');
subplot(1, 3, 3);
imagesc(data3);
title('Subplot 2');
% Create a color bar
cbar = colorbar;
cbar_title = 'Global Color Bar';
cbar.Label.String = cbar_title;
0 Comments
Accepted Answer
Florian Bidaud
on 8 Aug 2023
Edited: Florian Bidaud
on 8 Aug 2023
Hi
% Sample data for the subplots
data1 = rand(10, 10);
data2 = rand(10, 10);
data3 = rand(10, 10);
% Set the default font size for all subplots in the figure
set(0, 'DefaultAxesFontSize', 20);
% Create a figure
figure
% Create tiled layout
tiledlayout(1,3)
% Create the first subplot
nexttile
imagesc(data1);
title('Subplot 1');
% Create the second subplot
nexttile
imagesc(data2);
title('Subplot 2');
% Create the third subplot
nexttile
imagesc(data3);
title('Subplot 3');
% Create a color bar
cbar = colorbar;
cbar_title = 'Global Color Bar';
cbar.Label.String = cbar_title;
However if you want to keep colorbar, you can change the size and the position of every element of your plot with the attribute 'Position', see :
Position = [x,y,size_x,size_y]
(x,y) are the bottom left coordinates
(size_x,size_y) are the size of the element on x and y axis.
Here you want size_x of your third subplot to be the same as the others. Then you need to adjust the other elements for a nice presentation
figure;
% Create the first subplot
s1=subplot(1, 3, 1);
imagesc(data1);
title('Subplot 1');
% Create the second subplot
s2=subplot(1, 3, 2);
imagesc(data2);
title('Subplot 2');
s3=subplot(1, 3, 3);
imagesc(data3);
title('Subplot 3');
% Create a color bar
cbar = colorbar;
cbar_title = 'Global Color Bar';
cbar.Label.String = cbar_title;
s3.Position(3) = s2.Position(3);
s1.Position(1) = s1.Position(1) - 0.07;
s2.Position(1) = s2.Position(1) - 0.07;
s3.Position(1) = s3.Position(1) - 0.07;
0 Comments
More Answers (0)
See Also
Categories
Find more on Bar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!