- Set an explicit x-range and set it at the end of your loop with the xlim function.
- 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.
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?
39 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
michael ambler
le 28 Juin 2019
Commenté : michael ambler
le 28 Juin 2019
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
0 commentaires
Réponse acceptée
Rik
le 28 Juin 2019
You have two options:
Plus de réponses (1)
Bjorn Gustavsson
le 28 Juin 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
Voir également
Catégories
En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!