# DateListPlot from DateHistogram

How do I draw a DateListPlot from the bin height and bin middle position? I have a series of time events from which I calculate DateHistogram, but I want to represent the result through a kind DateListPlot, with joined line.

Example:

data = {DateObject[{2015, 6, 10}], DateObject[{2015, 6, 26}],
DateObject[{2015, 6, 15}], DateObject[{2015, 6, 25}],
DateObject[{2015, 6, 23}], DateObject[{2015, 6, 19}],
DateObject[{2015, 5, 31}], DateObject[{2015, 6, 6}],
DateObject[{2015, 6, 4}], DateObject[{2015, 6, 16}],
DateObject[{2015, 5, 29}], DateObject[{2015, 6, 4}],
DateObject[{2015, 6, 14}], DateObject[{2015, 6, 3}],
DateObject[{2015, 6, 14}], DateObject[{2015, 6, 6}],
DateObject[{2015, 6, 10}], DateObject[{2015, 6, 19}],
DateObject[{2015, 6, 20}], DateObject[{2015, 6, 21}]};

histogram = DateHistogram[data, "Day"]

How do I convert it to a kind of DateListPlot with joined line, where each point is the bin height and bin middle position?

You may use Reap and Sow with the functional height syntax of DateHistogram.

{dh, {{bins, counts}}} =
Reap[DateHistogram[data, "Day", Function[{b, c}, Sow[b]; Sow[c]]]];

Show[dh, DateListPlot[Select[Last@# != 0 &]@Transpose@{Mean /@ bins, counts}]]

This uses the functional height syntax to return the bins and counts that DateHistogram has calculated internally. You can think of it as hooking the internal date version of HistogramList. It returns the counts (c) without altering them so you get the expected histogram; b are the bins.

There is similar code in Histogram's documentation in the Application section's Create a ListLinePlot using counts extracted from a histogram.

Hope this helps.

You want some kind of DateHistogramList function that works like HistogramList does, but I don't see an option for it. Without that, you could just extract the coordinates for the tops of the Rectangles in the histogram.

histogramDatelistplot =
Cases[histogram,
Rectangle[{x1_, y1_}, {x2_, y2_}, __] |
RectangleBox[{x1_, y1_}, {x2_, y2_}, __] :> {Mean[{x1, x2}], y2},
Infinity] // DateListPlot

The code needed to modified because for some bin specifications (like "Day"), the histogram uses Rectangle while for others ("Month") it uses `RectangleBox". You can show them together,

Show[histogram, histogramDatelistplot]

• Ok, thanks. Even so why is your example is not working when the option "Day" to "Month"? Jun 21, 2016 at 19:07
• To figure that out, look at the results from histogram // InputForm - this will show the full code needed to produce the plot. For some unknown reason, when you use "Month" it uses RectangleBox instead of Rectangle. So what you need to do is change Rectangle[{x1_, y1_}, {x2_, y2_}, __] to Rectangle[{x1_, y1_}, {x2_, y2_}, __] | RectangleBox[{x1_, y1_}, {x2_, y2_}, __] and now it will match both patterns. Jun 21, 2016 at 19:12
• Ok, it's working now. Thanks! Jun 21, 2016 at 19:36

You can use the third argument of DateHistogram to extract bins and heights and use them in DateListPlot as Epilog:

DateHistogram[data, "Day", (((dl = Transpose[{Mean/@#, #2}]); #2)&),
Epilog -> First[DateListPlot[dl, PlotStyle -> Directive[Thick, Red]]]]

If you want to remove bars with zero heigth, use

DateHistogram[data, "Day", (((dl = Transpose[{Mean /@ #, #2}]); #2)&),
Epilog -> First[DateListPlot[DeleteCases[dl, {_, 0}], PlotStyle->Directive[Thick, Red]]]]