필터 지우기
필터 지우기

Two sets of data with two date ranges and y-axis values

조회 수: 1 (최근 30일)
BenSven
BenSven 2024년 3월 6일
댓글: Star Strider 2024년 3월 6일
Hello all
I have two sets of data in Excel which I have uploaded to Matlab.
I want to plot these data on one grpah as a line/scatter graph.
Data Set 1
This has days on the x axis from day1 to day 250.
The value on the y axis goes up to 20 metres. I want to put this on the left side of the plot which is standard.
Data Set 2
This also has days on the x axis but the data only starts on day 15 and also goes up to 250.
The value on this y-axis for this data goes from 90 - 200 metres. I want to put this on the right side of the plot (Command = yyaxis right?)
I want to plot these two on the same plot but Data Set 2 values should only start on day 10 of Data Set 1
How do I get the data points to line up correctly?
Like this :
plot
code
hold on
code
I'm not sure if I have explained this correctly so please let me know if you need more of an explanation.
Many thanks

채택된 답변

Star Strider
Star Strider 2024년 3월 6일
편집: Star Strider 2024년 3월 6일
Try this —
x1 = 1:250;
y1 = 20*sin(2*pi*x1/100) + 20 + randn(size(x1))*5;
x2 = 15:250;
y2 = cos(2*pi*x2/150)*60 + 150 + randn(size(x2))*10;
figure
yyaxis left
plot(x1, y1)
ylabel('Y1')
yyaxis right
plot(x2, y2)
xl = xline(15,'--k', '‘(x2,y2)’ Start Time');
xl.LabelVerticalAlignment = 'bottom';
xl.LabelHorizontalAlignment = 'center';
ylabel('Y2')
grid
xlabel('X')
EDIT — (6 Mar 2024 at 19:02)
Added xline call to emphasize when ‘(x2,y2)’ begins.
.
  댓글 수: 4
BenSven
BenSven 2024년 3월 6일
Thank you. I think I have figured it out.
Very much appreciated.
Star Strider
Star Strider 2024년 3월 6일
As always, my pleasure!

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

추가 답변 (1개)

Aquatris
Aquatris 2024년 3월 6일
편집: Aquatris 2024년 3월 6일
Something like this maybe:
% create dummy time and data vectors
t1 = datetime(2013,11,1,8,0,0); % define random start of time vector
t2 = datetime(2014,11,1,8,0,0); % define random end of time vector
t_data1 = t1:t2; % time vector for data1
t_data2 = t_data1(120:end); % time vector for data2 that
% starts at a later date than time vector 1
data_1 = rand(length(t_data1),1)*20; % random data 1
data_2 = rand(length(t_data2),1)*200;% random data 2
% desired plot maybe?
yyaxis left
plot(t_data1,data_1,'b.') % plot data 1 with blue dots on left scale
ylabel('data 1')
yyaxis right
plot(t_data2,data_2,'r.') % plot data 2 with red dots on right scale
ylabel('data 2')
legend('data 1','data 2')

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by