How can I plot two different datetime vectors in the same graph?

Hi All,
I have two datetime vectors (time1 and time2) with different sample period and different lengths.
Each of these vectors corresponds to different variable. For example - the first vector (time1) corresponds to to water velocity (V) in a river, and the second (time2) to rain intensity (R).
Since they cover the same time range (April to November, 2018) I would like to plot them on the same graph. However, I don't seem to manage doing so, probably because they differ in length and sample frequency.
Could anyone share any solution to that?
Thanks, Ron

7 件のコメント

Caglar
Caglar 2018 年 11 月 9 日
Hello, did you try plot(...); hold on; plot(...)?
Walter Roberson
Walter Roberson 2018 年 11 月 9 日
Or
plot(time1, V, time2, R)
Ron Nativ
Ron Nativ 2018 年 11 月 9 日
This doesn't seem to work. See the attached graph. Also notice the X axis which is not in a datetime format (although the vectors are!)
madhan ravi
madhan ravi 2018 年 11 月 9 日
plotyy?
Caglar
Caglar 2018 年 11 月 9 日
@Ron Nativ It seems to me that problem is about something else. Can you post more of your code, or better, a minimum working example.
Walter Roberson
Walter Roberson 2018 年 11 月 9 日
Which MATLAB version are you using?
Ron Nativ
Ron Nativ 2018 年 11 月 9 日
Thank you all for answering, Caglar, you are right, the problem was not related to the specific graphic coding, but to my data handling. Sorry for that and thanks again!

サインインしてコメントする。

回答 (2 件)

Caglar
Caglar 2018 年 11 月 9 日
編集済み: Caglar 2018 年 11 月 9 日

0 投票

Comment if any part is not clear.
first_date_array=datetime('today'):days(1):datetime('today')+days(10); %array for 10 days
first_data=1:1:11;
second_date_array=datetime('today'):days(2):datetime('today')+days(15); %array for 15 days and it has data for each 2nd day only
second_data=8:-1:1;
figure(1); %method 1: plot them together
plot(first_date_array,first_data,'o',second_date_array,second_data,'o'); %x,y,markershape,x,y,markershape
xlabel('Datetime'); ylabel('Data')
legend('First','Second')
figure(2); %method 2: add new plots using hold on
plot(first_date_array,first_data,'o');
hold on
plot(second_date_array,second_data,'o');
xlabel('Datetime'); ylabel('Data')
legend('First','Second')
madhan ravi
madhan ravi 2018 年 11 月 9 日
編集済み: madhan ravi 2018 年 11 月 9 日

0 投票

Or try
yyaxis right
plot(time1, V)
yyaxis left
plot(time2, R)
Also notice the X axis which is not in a datetime format (although the vectors are!)
xtickformat('dd-MMM-yyyy')
An example:
t = datetime(2014,6,28) + calweeks(0:9);
y = rand(1,10);
yyaxis left
plot(t,y);
y = 1:10;
yyaxis right
plot(t,y(1:numel(t)));
AX=findall(0,'type','axes');
AX(2) %grabs second axis handle experiment and see by scrolling the graph
xtickformat(AX,'dd-MMM-yyyy')

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

質問済み:

2018 年 11 月 9 日

コメント済み:

2018 年 11 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by