How to plot datetime array?

78 vues (au cours des 30 derniers jours)
Asher Zaidi
Asher Zaidi le 18 Juin 2018
Commenté : Bucks Lin le 14 Avr 2022
I would like to plot a vector, r, as a function of time. The two vectors I have are:
r'
1.0e+03 *
0.0179
0.0180
0.0184
0.0184
0.0189
0.0189
0.0189
0.0189
0.0190
0.0190
ds =
10×1 datetime array
02-Feb-2018 11:08:11
02-Feb-2018 12:08:13
02-Feb-2018 01:08:14
02-Feb-2018 02:08:15
02-Feb-2018 03:08:17
02-Feb-2018 04:08:18
02-Feb-2018 05:08:20
02-Feb-2018 06:08:21
02-Feb-2018 07:08:23
02-Feb-2018 08:08:24
However, when I try to plot them:
plot(datenum(ds), r, '-o')
datetick('x', 'dd-mmm-yyyy HH:MM:SS')
I get this graph (see attached). I would like for each point to have a corresponding time and date on the x-axis, if it is possible.

Réponse acceptée

Walter Roberson
Walter Roberson le 18 Juin 2018
plot(ds, rs, '-o')
From R2016b onward, you can use datetime objects as the x or y axes. (Exact release varies a bit with variety of graph being plotted.)
  3 commentaires
Walter Roberson
Walter Roberson le 19 Juin 2018
You could get that if you were trying to do a datetime plot on top of an existing numeric axes.
If you are trying to draw on top of an existing axes, then you might have "hold on" in effect, and that might have affected the xtick values that the axes thought should be used.
When I use your commands together with
ds = datetime({'02-Feb-2018 11:08:11'
'02-Feb-2018 12:08:13'
'02-Feb-2018 01:08:14'
'02-Feb-2018 02:08:15'
'02-Feb-2018 03:08:17'
'02-Feb-2018 04:08:18'
'02-Feb-2018 05:08:20'
'02-Feb-2018 06:08:21'
'02-Feb-2018 07:08:23'
'02-Feb-2018 08:08:24'});
r = 1e3 * [ 0.0179
0.0180
0.0184
0.0184
0.0189
0.0189
0.0189
0.0189
0.0190
0.0190];
then xtick creates labels for every 3 hours.
Asher Zaidi
Asher Zaidi le 20 Juin 2018
I see, thank you!

Connectez-vous pour commenter.

Plus de réponses (1)

Aron Magesh
Aron Magesh le 22 Mai 2020
If you have used hold on function, make sure to use the hold off as well to avoid this error.
  1 commentaire
Bucks Lin
Bucks Lin le 14 Avr 2022
wow!
thanks Aron!! your tips save my day.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by