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

조회 수: 35 (최근 30일)
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)')

채택된 답변

Star Strider
Star Strider 2020년 9월 20일
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
Heidi Hirsh
Heidi Hirsh 2020년 9월 21일
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 2020년 9월 22일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by