How to match scale of x axes in subplots, so that a given time period occupies same amount of x axis space in each subplot?

40 views (last 30 days)
I'm trying to generate a plot containing subplots of experiments with time on the x axis. Each experiment lasted a different amount of time, but in order to be able to visually compare one experiment (subplot) with another, I want the x axes to scale the same rather than stretch to fit the same amount of subplot space.
My (probably quite clunky) code is here:
clear
clc
LightsOff = 8.5; %set time of lights off here
smoothWindow = 648; % set smoothing window (no. frames at ~1.8Hz)
yl = ([20 40]);
NumberOfPlots = 2;
ID = 'O1';
%%
figure
hold on
for r = 1: NumberOfPlots
TempFile = [ID,' temp file ',num2str(r),'.txt'];
data = readtable(TempFile);
time = table2cell(data(:,2));
time = datenum(time);
d = datetime(time(1),'ConvertFrom','datenum');
d.Format = 'dd-MMM-yyyy';
date = cellstr(d);
date = datenum(date);
CorrectedTime = (time-date)*24; %converts to number of hours past midnight on day of recording
temp =cell2mat(table2cell(data(:,3)));
TF1 = temp<40 & temp>=20;
temp2 = temp(TF1,:);
time2 = CorrectedTime(TF1,:);
temp2 = smooth(temp2,smoothWindow);
% build patches
% first section picks out lights on/off from each day
subplot(1,NumberOfPlots,r);
hold on
axis tight
NoDays = floor(time2(end)/24); %number of complete days
PatchY = [yl(1) yl(1) yl(2) yl(2)];
PatchX = [];
for i = 1:NoDays+1;
PatchX(i) = LightsOff+((i-1)*24);
end
for i = 1:length(PatchX)
PatchX2 = [PatchX(i) PatchX(i)+12 PatchX(i)+12 PatchX(i)];
PatchX3 = [PatchX(i)+12 PatchX(i)+24 PatchX(i)+24 PatchX(i)+12];
hold on
patch(PatchX2,PatchY,[0.8 0.8 0.8], 'EdgeColor', 'none');
hold on
patch(PatchX3,PatchY,[0.95 0.95 0.95], 'EdgeColor', 'none');
end
if time2(1) < LightsOff
PatchX1 = [time2(1) LightsOff LightsOff time2(1)];
hold on
patch(PatchX1,PatchY,[0.95 0.95 0.95], 'EdgeColor', 'none');
else
end
plot(time2, temp2);
set(gca, 'Layer', 'top');
end
hold off

Accepted Answer

Rik
Rik on 28 Jun 2019
You have two options:
  1. Set an explicit x-range and set it at the end of your loop with the xlim function.
  2. In your current code store the xlim to a variable and after your current loop add a second loop where you set the xlim for each axes.
  1 Comment
michael ambler
michael ambler on 28 Jun 2019
Thanks Rik, I guess I pick out the longest duration subplot and scale them all according to that one..?
Was lazily hoping there was a function I could call but you're right this should do the trick.
Cheers

Sign in to comment.

More Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 28 Jun 2019
For this you can go a number of routes, one of the simlper would be to change the subplot call to:
sph(r) = subplot(1,NumberOfPlots,r);
Then after the look you could link the axes:
linkaxes(sph,'x')
set(gca,'xlim',[t_of_interest_start t_of_interest_end])
HTH
  1 Comment
michael ambler
michael ambler on 28 Jun 2019
Thanks Bjorn, I may be doing it wrong but I tried this before submitting the question and while it did match the scales it also massively increased the empty regions of the siubplots, I was hoping that it might be possible to change the sizes of the subplots so that the scales matched (ie shorter duration experiments have subplots that occupy less space in the x axis).

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!