Plot changes when using datetime

2 Ansichten (letzte 30 Tage)
Lisa Zell
Lisa Zell am 7 Feb. 2021
Kommentiert: the cyclist am 7 Feb. 2021
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).

Antworten (1)

the cyclist
the cyclist am 7 Feb. 2021
Bearbeitet: the cyclist am 7 Feb. 2021
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 Kommentare
Lisa Zell
Lisa Zell am 7 Feb. 2021
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 am 7 Feb. 2021
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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by