Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have date and time data and want to plot its frequency histogram. Ideally I want to be able to customize the bin size (for example, every 5 minutes or a day). Anybody has this experience?

share|improve this question
Care to show the code you are working on/with ? –  Sektor Sep 2 '13 at 18:16
Related: mathematica.stackexchange.com/q/2333 –  Brett Champion Sep 3 '13 at 3:24

2 Answers 2

up vote 3 down vote accepted

In version 7 I don't have the fancy functions Verbeia used. One can still process the data as any other. Gather and GatherBy, as Verbeia alluded to, are good options.

For example, starting with some random date data in DateList format:

range = AbsoluteTime /@ {{2013}, {2014}}; dates = DateList /@ RandomInteger[range, 5000];

And using DateListPlot with CapForm["Butt"]:

plot[dat_, style__] :=
  DateListPlot[dat, PlotStyle -> None, Filling -> Bottom, 
   FillingStyle -> Directive[CapForm["Butt"], style]]

We could bin by day:

bins = {#[[1]], Length@#} & /@ Sort@Gather[dates[[All, ;; 3]]];

plot[bins, Orange, Thickness[0.001]]

enter image description here

By calendar weeks:

bins = {#[[1]] + {0, 0, 3.5}, Length@#} & /@ 
          Sort @ Gather[{#, #2, Floor[#3, 7]} & @@@ dates];

plot[bins, Orange, Thickness[0.01]]

enter image description here

By absolute seven-day periods:

week = 60^2 * 24 * 7;

bins = {#[[1]] + week/2, Length@#} & /@ 
         Sort @ Gather @ Floor[AbsoluteTime /@ dates, week];

plot[bins, Orange, Thickness[0.01]]

enter image description here

By ten-minute intervals (with data covering a single day):

range = AbsoluteTime /@ {{2013, 9, 1}, {2013, 9, 2}};
dates = DateList /@ RandomInteger[range, 5000];

bins = {#[[1]] + {0, 0, 0, 0, 5}, Length@#} & /@
   Sort @ Gather[{#, #2, #3, #4, Floor[#5, 10]} & @@@ dates];

plot[bins, Orange, Thickness[0.004]]

enter image description here

share|improve this answer
This is really nice, since I don't have Mma 9. But does this work for time range rather than date range? –  Qiang Li Sep 3 '13 at 1:05
@QiangLi Do you mean by hours of the day? As in bins: (0:01, 6:00), (6:01, 12:00), (12:01, 18:00), (18:01, 24:00) or similar? –  Mr.Wizard Sep 3 '13 at 1:10
Yes. Or even by 10 minutes, when I have a lot of data. Oh, just upvoted. :) –  Qiang Li Sep 3 '13 at 1:11
@QiangLi Added. –  Mr.Wizard Sep 3 '13 at 1:25

Someone else will probably come up with something involving GatherBy, but I would like to showcase the undocumented TemporalData functions I recently posted about.

You could convert your time series (list of date-value pairs) into TemporalData and use the Aggregate function in the TemporalData` package like this:

timeAggregate[data_,basis_,method_:Mean] (* single series, or TemporalData head *):=
With[{tddata= If[Head[data]===TemporalData,data,TemporalData[data]]},

where the message would be something like:

timeAggregate::nottd = "The data provided is not or cannot be converted to valid TemporalData."

This function converts the date-value pair into TemporalData if it is not already in TemporalData form, tests that it is valid TemporalData (using another undocumented function in that package), and then aggregates the data according to the time specification basis. The aggregation method method can be any function that returns a scalar from a list of values, but I presume you want totals.

Usage would be along the lines of:

timeAggregate[mydata,{5, "Minute"},Total]

So for example:

mydata = Transpose@{AbsoluteTime@DateList[] + Range[4000], 
RandomVariate[NormalDistribution[0, 1], 4000]};

aggred = timeAggregate[mydata, {5, "Minute"}, Total];

You could then strip off the dates to use BarChart directly, or alternatively use the method described by Mr.Wizard here to make a quick-and-dirty bar chart of dated data:

DateListPlot[aggred, Filling -> Axis, PlotStyle -> None, 
 FillingStyle ->  Directive[Orange, CapForm["Butt"], AbsoluteThickness[15]], 
 Axes -> True]

enter image description here

share|improve this answer
Thanks for the attribution. –  Mr.Wizard Sep 3 '13 at 0:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.