how to add datetime variable to a bar graph
Afficher commentaires plus anciens
I have a datetime variable with that is 30x1 representing 30 days. The original datetime variable has yyyy-MM-dd HH-mm-ss. How can I create a bar graph with the x-axis having the datetime values? I can do it with plot but not bar.
Réponse acceptée
Plus de réponses (1)
Peter Perkins
le 21 Juil 2017
Starting from R2016b, plotting supports datetimes much more widely, and natively. So
>> d = datetime(2017,1:10,1);
>> x = rand(1,10);
>> bar(d,x)

Prior to R2016b, you'd have to make the bar chart with something like 1:10 for the horizontal axis locations, and then set the tick labels by calling cellstr on your datetimes (or use datetick).
7 commentaires
I figured would get a round tuit eventually, Peter..I'll try to recall the time frame going forward but no promises! :)
It still is a double datenum internally to the axes properties, though, right?
"...set the tick labels by calling cellstr on your datetimes"
Ah! Hadn't actually thought of doing that particular workaround for this particular issue. For bar undoubtedly there will be the right tick marks for some of the others there can be ticks elsewhere by default so do have to ensure they coincide in number and intended labels. Thus why I went the datetick route above; it does associate with the actual ticks transparently to the user.
Peter Perkins
le 24 Juil 2017
Note the datetimes as the values of the XLim and XTick properties:
>> d = datetime(2017,1:10,1);
>> x = rand(1,10);
>> bar(d,x)
>> ax = gca
ax =
Axes with properties:
XLim: [Nov 28, 2016, 09:36 Nov 03, 2017, 14:24]
YLim: [0 1]
XScale: 'linear'
YScale: 'linear'
GridLineStyle: '-'
Position: [0.13 0.11 0.775 0.815]
Units: 'normalized'
Show all properties
>> ax.XTick
ans =
1×10 datetime array
Columns 1 through 9
01-Jan-2017 01-Feb-2017 01-Mar-2017 01-Apr-2017 01-May-2017 01-Jun-2017 01-Jul-2017 01-Aug-2017 01-Sep-2017
Column 10
01-Oct-2017
dpb
le 24 Juil 2017
Getting there! :) That will be handy if I can ever get to a recent-enough release.
Will still accept datenum s, too?
Peter Perkins
le 25 Juil 2017
It doesn't. You're probably thinking backwards compatibility for code written in the 14b-16a window that actually messed with certain figure properties from plot. Such code exists, and short-term, that might seem like a good idea. Long-term, I think we'd all end up hating it.
dpb
le 25 Juil 2017
I was thinking of backward compatibility, yes, but was much more simple-minded in doing so! :)
I had noticed that with R2014b (again, where I'm stuck at moment owing to hardware limitations) plot with a datetime argument returns the limits as datenums instead and therefore users who mess with axes limits had to write in those units to manipulate the details.
If one had code already written but were to extend it, wondered it those would continue to work but able to reference the ruler object instead of the upper-level axis limit, tick and other properties or whether all such machinations would also have to be rewritten besides just whatever was the additional extension desired.
But, mayhaps those are the things your description above is speaking of... ?
Peter Perkins
le 26 Juil 2017
Correct.
dpb
le 26 Juil 2017
OK. So to see if I'm on track...the two incantations of plot in graph2d and datetime say actually now end up having different properties accessible, right?
Catégories
En savoir plus sur Dates and Time dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!