MATLAB Answers

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

6 views (last 30 days)
Heidi Hirsh
Heidi Hirsh on 20 Sep 2020
Commented: Star Strider on 22 Sep 2020
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
figure
subplot(2,1,1)
hold on; box on;
for i= 1:length(days)
plot(MX1day(days{i}),cutflux(days{i}),'.','color',colors(i,:),'markersize',10)
end
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')
set(gca,'fontsize',20)
ylabel('O_2 Flux (mmol/m^2/hr)')
datetickzoom
subplot(2,1,2)
hold on; box on;
plot(1:24,mfbin,'k','linewidth',3)
xlim([0 24])
ylim([-150 150])
set(gca,'fontsize',20)
ylabel('O_2 Flux (mmol/m^2/hr)')

  0 Comments

Sign in to comment.

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);
figure
plot(DT, cutflux, '.')
hold on
plot(hrs, mfbin, '-k', 'LineWidth',3)
hold off
xtickformat = 'HH';
producing:
I have no idea if this will work for all your days, since I don’t have them to experiment with. It works here.

  8 Comments

Show 5 older comments
Star Strider
Star Strider on 21 Sep 2020
My pleasure!
This gets the shading correct, considering the limitations of your data, however the rest may not be possible:
DT = datetime(MX1day,'ConvertFrom','datenum');
hrs = min(DT):hours(1):max(DT);
figure
plot(DT, cutflux, '.')
hold on
plot(hrs, mfbin, '-k', 'LineWidth',3)
fill([hrs(1:8), fliplr(hrs(1:8))], [ones(1,8)*min(ylim) ones(1,8)*max(ylim)], 'b', 'FaceAlpha',0.2)
fill([hrs(19:24), fliplr(hrs(19:24))], [ones(1,6)*min(ylim) ones(1,6)*max(ylim)], 'b', 'FaceAlpha',0.2)
hold off
.
Heidi Hirsh
Heidi Hirsh on 21 Sep 2020
Thank you!
I like how you redefined hrs. Oddly it looks like the second interval now shades 18:00 to 23:00 instead of 19:00 to 24:00
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);
figure
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!