Aligning subplots (width) in a figure

2 views (last 30 days)
Jane
Jane on 16 Dec 2013
Commented: Jane on 16 Dec 2013
Hello, I'd like to align the x axes of my two subplots. I can adjust it a little bit, but I'd like it to match up perfectly. Help? Thanks!
%%Graphing Fluorescent Intensity
clc;
clear all;
close all;
fontSize = 16;
%
% Calculate the mean gray level.
grayImage = imread('alignedImage.png');
meanAlongEachColumn = mean(grayImage);
%
% Plot the aligned image
h=figure;
subplot(2,1,1);
alignPlot = subplot(2,1,1);
topAxs = gca;
photoAxsRatio = get(topAxs,'PlotBoxAspectRatio');
imshow('alignedImage.png');
axis on;
title('Aligned Image', 'FontSize', fontSize);
%
% Plot the Fluorsecent Intensity
subplot(2,1,2);
fluorPlot = subplot(2,1,2);
botAxs = gca;
%
plot(meanAlongEachColumn, 'k-', 'LineWidth', 2);
title('Fluorescent Intensity', 'FontSize', fontSize);
xlabel('Position');
ylabel('Fluorescent Intensity');
% adjust ratios
botAxsRatio = photoAxsRatio;
botAxsRatio(2) = photoAxsRatio(2)/1.88; % NOTE: not exactly...
set(botAxs,'PlotBoxAspectRatio', botAxsRatio)
%
% Find current position [x,y,width,height]
pos1 = get(alignPlot, 'Position');
pos2 = get(fluorPlot, 'Position');
%
% Set width of second axes equal to first
pos2(3) = pos1(3);
set(alignPlot,'Position',pos1);
% Save plot
saveas(h,'graphfluor.png');
end

Answers (1)

David Sanchez
David Sanchez on 16 Dec 2013
Get the size of your image:
[x_size y_size] = size(grayImage);
Then, right after the second subplot:
axis([0 x_size 0 12]);
  1 Comment
Jane
Jane on 16 Dec 2013
Thanks but this gives me
if true
% %%Graphing Fluorescent Intensity
clc;
clear all;
close all;
fontSize = 16;
%
% Calculate the mean gray level.
grayImage = imread('alignedImage.png');
[x_size y_size] = size(grayImage);
meanAlongEachColumn = mean(grayImage);
%
% Plot the aligned image
h=figure;
subplot(2,1,1);
imshow('alignedImage.png');
axis on;
title('Aligned Image', 'FontSize', fontSize);
%
% Plot the Fluorsecent Intensity
subplot(2,1,2);
plot(meanAlongEachColumn, 'k-', 'LineWidth', 2);
title('Fluorescent Intensity', 'FontSize', fontSize);
xlabel('Position');
ylabel('Fluorescent Intensity');
axis([0 x_size 0 12]);
%
% Save plot
saveas(h,'graphfluor2.png');
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!