How can I plot two datasets together if they have different x axis values?

24 views (last 30 days)
I want to plot flux data versus time. I have a dataset of flux values where the x-axis is timestamps (not chronological because I forced the dates to be the same so that my 17-day data shows up as one composite day). I have another vector of hourly mean flux values. I want to plot these as a line over the flux data but the x axis data is not the same as the x coordinates for the individual flux values. I don't know how to tell Matlab to plot both together. I think I need to convert the x coordinates for one dataset into the format of the other but I'm not sure how to do that.
This is what the data looks like
Data 1: x = MX1day (4888x1 double), y = cutflux (4888x1 double)
Data 2: x = 1:24, y = mfbin (24x1 double)
I have attached data.mat which includes MX1day, cutflux, and mfbin
I plotted the data in two separate subplots (see attached). Hopefully this makes it easier to see my problem. The hour numbers from the lower plot should be the same as the hourly ticks about but I'm not sure how to tell it to plot that way on the same axes.
This is how I am plotting the attached figure
hold on; box on;
for i= 1:length(days)
legend('July 23','July 24','July 25','July 26','July 27','July 28','July 29','July 30','July 31',...
'Aug 1','Aug 2','Aug 3','Aug 4','Aug 5','Aug 6','Aug 7','Aug 8','Aug 9')
ylabel('O_2 Flux (mmol/m^2/hr)')
hold on; box on;
xlim([0 24])
ylim([-150 150])
ylabel('O_2 Flux (mmol/m^2/hr)')

Accepted Answer

Star Strider
Star Strider on 20 Sep 2020
The ‘data.mat’ file contains data for only one day as datenum values, that apparently are all for 01-Jan-2020.
One approach:
D = load('data.mat');
MX1day = D.MX1day;
cutflux = D.cutflux;
mfbin = D.mfbin;
DT = datetime(MX1day,'ConvertFrom','datenum');
hrs = min(DT)+hours(1:24);
plot(DT, cutflux, '.')
hold on
plot(hrs, mfbin, '-k', 'LineWidth',3)
hold off
xtickformat = 'HH';
I have no idea if this will work for all your days, since I don’t have them to experiment with. It works here.
Star Strider
Star Strider on 22 Sep 2020
As always, my pleasure!
I needed to take a break.
This gets closer:
DT = datetime(MX1day,'ConvertFrom','datenum');
hrs = min(DT):minutes(1):max(DT);
plot(DT, cutflux, '.')
hold on
plot(hrs(1:60:end), mfbin, '-k', 'LineWidth',3)
fill([hrs(1:420), fliplr(hrs(1:420))], [ones(1,420)*min(ylim) ones(1,420)*max(ylim)], 'b', 'FaceAlpha',0.2)
fill([hrs(end-299:end), fliplr(hrs(end-299:end))], [ones(1,300)*min(ylim) ones(1,300)*max(ylim)], 'b', 'FaceAlpha',0.2)
hold off
The rest of the code is unchanged.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!