how to add datetime variable to a bar graph
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Akzeptierte Antwort
dpb
am 20 Jul. 2017
Bearbeitet: dpb
am 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.
0 Kommentare
Weitere Antworten (1)
Peter Perkins
am 21 Jul. 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 Kommentare
dpb
am 26 Jul. 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?
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!