how to add datetime variable to a bar graph

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.

 Accepted Answer

dpb
dpb on 20 Jul 2017
Edited: dpb on 21 Jul 2017
For some reason TMW hasn't yet gotten around to making the rest of the plotting routines datetime aware so you've got to revert to the venerable datenum(*):
Try
dt=datenum(2017,1,1:4).'; % a short time series vector
y=randn(4,30); % some y-data to go along with it
hBar=bar(dt,y); % bar graph vs the time
datetick('x','keeplimits','keepticks') % set the axis to display time strings
See
doc datetick % and friends
for the details on formatting, etc., ...
ADDENDUM
() *NB: Even with plot using a datetime- object, internal to axes the date values on the axis are datenum doubles, not datetime objects. TMW pretty-much had to do this for compatibility purposes. Hence, while there's a "pretty" user interface and the new class/object has some useful properties, the old-style datenum isn't going to go away any time soon.

More Answers (1)

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 Comments

dpb
dpb on 21 Jul 2017
Edited: dpb on 21 Jul 2017
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.
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
Getting there! :) That will be handy if I can ever get to a recent-enough release.
Will still accept datenum s, too?
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.
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... ?
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?

Sign in to comment.

Categories

Asked:

on 20 Jul 2017

Commented:

dpb
on 26 Jul 2017

Community Treasure Hunt

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

Start Hunting!