My plot changes when I try to use a datetime vector for the x-axis. My y-value is a normal double vector. Why does my plot change when I use the datetime vector? First picture is plot(y). Second picture is plot(datetime,y).

回答 (1 件)

the cyclist
the cyclist 2021 年 2 月 7 日
編集済み: the cyclist 2021 年 2 月 7 日

1 投票

It would normally be helpful for you to upload the data so we could investigate, but in this case I think it is pretty clear.
If you have, say, 100 data points in your y vector, then the command
plot(y)
will plot that y vector against
x = 1:100
and therefore each of your data points will be equally spaced along the x-axis.
But when you plot against datetime, then your datetime values must very differently spaced from each other. (For example, some may be a few minutes apart, but some are a day or two.) So, all of the y values in your two plots are the same, but their spacing along the x-axis is very different.

2 件のコメント

Lisa Zell
Lisa Zell 2021 年 2 月 7 日
Thank you! That's the problem I guess. I am using stock market time data with a time gap between 4 p.m. and 9 a.m. the next day. Would you recommend to convert the dates to a text string and then match with the x-values?
the cyclist
the cyclist 2021 年 2 月 7 日
It depends on how you want the data displayed.
If you want the data displayed with the overnight time-gap, you could insert a NaN value in the middle of the night, which will break up the line. Here is a simple example of what I mean:
rng default
x = [1 2 3 4 nan 6 7 8 9];
y = [rand(1,4) nan rand(1,4)];
figure
plot(x,y,'.-')
If you do not want the time-gap, and instead want 9am on Tuesday to immediately follow 4pm Monday, then I think I would do something like what you are suggesting, on the original plot.

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

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2021 年 2 月 7 日

コメント済み:

2021 年 2 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by