How to make widths of all subplot and colorbars same?

11 views (last 30 days)
Following is my figure code. However, the size of subplots and colorbars are not same. How can I edit the following code to make all the subplots colorbars of same width. With following figure size, the x labels are appearing half below the figure boundary Can I also shift plots a little bit up?
figure,subplot(131); imagesc([3:0.01:11], time, Prob_ip0); hold on;
Unrecognized function or variable 'time'.
plot(ip0_m,time,'k','linewidth', 1.2); xlabel('I_P (km s^-1. kg m-3)');
axis([3 11 2000 2200]); set(gca,'ytick',[2000:40:2200]);
set(gca,'xtick',[3:4:11]); set(gca,'FontSize',11); grid on;
ylabel('Time (sec)'); colorbar; caxis([0 0.0061]);
subplot(132); imagesc([0.4:0.01:7], time, Prob_is0); hold on;
plot(is0_m,time,'k','linewidth', 1.2); xlabel('I_S (km s^-1. kg m-3');
axis([0.4 7 2000 2200]); set(gca,'ytick',[]); % set(gca,'ytick',[2000:40:2200]);
set(gca,'xtick',[0.4:3.5:7]); set(gca,'FontSize',11); grid on;
ylabel('Time (sec.)'); ylabel([]); colorbar; caxis([0 0.0076]);
subplot(133); imagesc([2.000:0.0010:2.600], time, Prob_rho0); hold on;
plot(rho0_m,time,'k','linewidth', 1.2); set(gca,'FontSize',11);
ylabel('Time (sec)'); ylabel([]); set(gca,'ytick',[]); %set(gca,'ytick',[2000:40:2200]);
xlabel('Rho (kg/m^3)'); set(gca,'xtick',[2.000:0.3:2.600]); grid on;
colorbar; colormap(flipud(jet)); caxis([0 0.0082]);
set(gcf, 'position', [500 285 800 410]);
  5 Comments
Matt J
Matt J on 10 Feb 2024
The more usual thing to do in this forum would be for you to run it for us, and explicitly demonstrate the problem.
Ahmed
Ahmed on 11 Feb 2024
@Matt J thanks for you comment, I find a solution by fixing the xaxix limits and ticks set(gca,'xtick',[0.4:3.5:7]); Now all the plots are of equal size.

Sign in to comment.

Accepted Answer

Austin M. Weber
Austin M. Weber on 10 Feb 2024
Someone else asked a very similar question to this last night (see this link).
Below is the answer that I provided them for setting the width of all the plots to be the same. You can write a local function that takes the axes handle for a subplot and set the width of the axes:
figure
s1 = subplot(7,1,1);
plot(rand(10),rand(10),'-');
s2 = subplot(7,1,2);
plot(rand(10),rand(10),'-');
s3 = subplot(7,1,3);
plot(rand(10),rand(10),'-');
colorbar
s4 = subplot(7,1,4);
plot(rand(10),rand(10),'-');
colorbar
s5 = subplot(7,1,5);
plot(rand(10),rand(10),'-');
colorbar
s6 = subplot(7,1,6);
plot(rand(10),rand(10),'-');
s7 = subplot(7,1,7);
plot(rand(10),rand(10),'-');
splots = [s1 s2 s3 s4 s5 s6 s7];
for n = 1:length(splots)
position_plot(splots(n))
end
function position_plot(ax)
set(ax,'Units','normalized');
set(ax,'Position',[ax.Position(1), ax.Position(2), 0.4, ax.Position(4)]); % You can change 0.4 (the width) to meet your needs
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!