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

1 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(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