How to set properly single colorbar for subplots?
111 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Florian Bidaud
2023-8-8
编辑:Florian Bidaud
2023-8-8
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Bar Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!