11
$\begingroup$

HistogramList and Histogram are closely related:

data = RandomVariate[NormalDistribution[], 200];
bSpec = {1};

{
 BarChart[Last[HistogramList[data, bSpec]]], 
 Histogram[data, bSpec]
}

enter image description here

DateHistogram is more like a black box with respect to that:

dates = AbsoluteTime /@  RandomChoice[DateRange[{2010, 1, 1}, {2010, 5, 31}, "Day"], 200];
 bSpec = "Month";


{
 BarChart[Last[HistogramList[dates, bSpec]]], 
 DateHistogram[dates, bSpec]
}

enter image description here

One could do

Reap[DateHistogram[dates, "Month", (Sow[{##}]; #2) &]][[2, 1, 1]]
 {{{3471292800, 3473971200}, {3473971200, 3476390400}, {3476390400, 

3479068800}, {3479068800, 3481660800}, {3481660800, 3484339200}}, {29, 29, 42, 47, 53}}

BarChart @ Last @ %

enter image description here

And tweak it but it is not pretty/efficient.

Question

Is there a better way to get HistogramList for dates based data? Compatible with DateHistogram?

$\endgroup$
2
  • $\begingroup$ "Month" is not a valid bin specification for HistogramList so you are getting the Automatic setting. You would need to create a bin function that would give a date (maybe AbsoluteTime) partition of the dates for HistogramList. $\endgroup$
    – Edmund
    Aug 10, 2016 at 21:09
  • $\begingroup$ @Edmund The point is I don't want to because this is already done. DateHistogram uses it. Would be also nice to know what Month means. $\endgroup$
    – Kuba
    Aug 10, 2016 at 21:13

1 Answer 1

7
$\begingroup$
dates = AbsoluteTime /@  RandomChoice[DateRange[{2010, 1, 1}, {2010, 5, 31}, "Day"], 200];
bSpec = "Month";

Didn't have time to dig deeper so it may be limited but it suits my needs atm.

Keep in mind that it is an undocumented function:

binSpec = System`TimeVisualizationsDump`dateHistogramBinning[
  dates,
  bSpec,
  Identity, (*smoothing function - at least the pattern name says so*)
  $TimeZone
  ]

hlist = HistogramList[dates, binSpec ];

{
  BarChart[
     #2, ChartLabels -> (DateString[#, "MonthNameShort"] & /@ #)
  ] & @@  hlist
  ,
  DateHistogram[dates, bSpec]
}

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ You sir, are a genius. Thank you so much. $\endgroup$
    – masterxilo
    Nov 20, 2018 at 8:57
  • $\begingroup$ @masterxilo long time no see. Thanks but that is an overreaction ;-) $\endgroup$
    – Kuba
    Nov 20, 2018 at 18:52
  • $\begingroup$ Mathematica has varying levels of applicability to the projects that come my way, but it always feels good to come back to this competent community of experts like yourself ;) $\endgroup$
    – masterxilo
    Nov 21, 2018 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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