Make x axis ticks and extent match

2 views (last 30 days)
Heidi Hirsh
Heidi Hirsh on 10 Dec 2018
Answered: Chad Greene on 10 Dec 2018
Hello, I am trying to plot timeseries data. Each data parameter was collected over a different length of time. I have successfully limited each plot to the same data collection range, however the ticks on my fourth subplot are not the same. Can anyone tell me why this is the case and how I might fix it?
This is my code:
close all;
f10 = figure(10);
%Define time to plot (for x axis)
t1 = datenum(2018,07,20,00,00,00);
t2 = datenum(2018,10,05,00,00,00);
subplot(4,1,1);%mfet ph
plot(mfet6.SDN, mfet6.pHint_prelim);
ylabel('mfet6 pH');
xlim([t1 t2]);
legend('14.57 mab','Location','northwest');
subplot(4,1,2);%minidot DO
plot(miniTtime, miniTDO);
hold on;
plot(miniMtime, miniMDO);
hold on;
plot(miniBtime, miniBDO);
ylabel('Dissolved Oxygen');
legend('Top (13.5 mab)','Middle (8.95 mab)','Bottom (1.07 mab)','Location','northwest');
ylim([4 15]);
xlim([t1 t2]);
subplot(4,1,3);%sbe37 salinity
ylim([33 34]);
xlim([t1 t2]);
legend('13.65 mab','Location','northwest');
ylim([0 200]);
xlim([t1 t2]);
legend('0 mab','Location','northwest');

Answers (2)

Star Strider
Star Strider on 10 Dec 2018
We can’t run your code. You appear to be using 4 different date vectors, one for each subplot. It may be worth exploring them to see how similar they are.
It could be easier to use datetick with arguments, such as the date format, as well as 'keepticks' and 'keeplimits'.

Chad Greene
Chad Greene on 10 Dec 2018
You're on the right track. If you want all the xticks to be the same, then you'll have to set them all to the same values. Currently, you get the automatic xtick values for each subplot, and then you set the string labels corresponding to those values. So I don't think that actually does anything. If you want subplots 2-4 to match subplot 1, then in subplot 1 do this:
Get the tick values and string labels in the first subplot:
xticklabels = get(gca,'xticklabel');
and then for each subsequent subplot, set the values to the same as subplot 1:
If there are specific dates you'd like to identify in each subplot instead of relying on the automatic ones in subplot 1, then you can simply set the xticks and xticklabels with those values for each subplot.

Community Treasure Hunt

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

Start Hunting!