Make x axis ticks and extent match

5 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');
datetick;
xlim([t1 t2]);
xticks=get(gca,'xtick');
set(gca,'xticklabel',cellstr(datestr(xticks))');
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]);
datetick;
xlim([t1 t2]);
xticks=get(gca,'xtick');
set(gca,'xticklabel',cellstr(datestr(xticks))');
subplot(4,1,3);%sbe37 salinity
plot(time37,sal37,'.');
ylabel('Salinity');
ylim([33 34]);
datetick;
xlim([t1 t2]);
xticks=get(gca,'xtick');
set(gca,'xticklabel',cellstr(datestr(xticks))');
legend('13.65 mab','Location','northwest');
subplot(4,1,4);%minipar
plot(time_par,PAR);
ylim([0 200]);
ylabel('PAR');
datetick;
xlim([t1 t2]);
xticks=get(gca,'xtick');
set(gca,'xticklabel',cellstr(datestr(xticks))')
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:
xticks=get(gca,'xtick');
xticklabels = get(gca,'xticklabel');
and then for each subsequent subplot, set the values to the same as subplot 1:
set(gca,'xtick',xticks,'xticklababel',xticklabels)
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!