4
$\begingroup$

I have a list of dates spanning over multiple years (i.e.):

datelist = {DateObject[{1995, 12, 13, 0, 0, 0.`}, "Instant", 
"Gregorian", 1.`], DateObject[{1996, 1, 18, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1996, 1, 19, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1996, 3, 1, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1996, 3, 24, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1996, 3, 25, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1997, 1, 22, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1997, 1, 31, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1997, 2, 22, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1997, 2, 22, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1997, 2, 23, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1997, 3, 15, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1997, 3, 16, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1997, 12, 10, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1997, 12, 12, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1997, 12, 19, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1997, 12, 21, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1998, 1, 30, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{1998, 3, 1, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2000, 1, 5, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2000, 1, 27, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2000, 1, 27, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2000, 1, 29, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2000, 3, 25, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2000, 10, 24, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2000, 12, 13, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2000, 12, 25, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2001, 1, 25, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2001, 2, 5, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2001, 12, 5, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2001, 12, 6, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2001, 12, 20, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2002, 1, 8, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2002, 1, 9, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2002, 1, 9, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2002, 2, 12, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2002, 3, 29, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2002, 4, 2, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2002, 4, 2, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2002, 4, 3, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2002, 12, 11, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2002, 12, 22, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2002, 12, 25, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2003, 2, 5, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2003, 2, 14, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2003, 2, 16, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2003, 2, 20, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2003, 2, 28, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2004, 12, 25, 0, 0, 0.`}, "Instant", "Gregorian", 1.`],DateObject[{2004, 12, 26, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2005, 1, 4, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2005, 1, 6, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2005, 2, 8, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2005, 3, 13, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2005, 11, 7, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2005, 12, 24, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2005, 12, 25, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2006, 1, 11, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2006, 1, 13, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2006, 1, 15, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2006, 12, 27, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2006, 12, 31, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2007, 1, 7, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2007, 3, 18, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2007, 11, 22, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2008, 1, 8, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2008, 2, 15, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2009, 2, 18, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2009, 2, 22, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2009, 2, 28, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2009, 3, 2, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2009, 3, 24, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2010, 1, 26, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2011, 1, 1, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2011, 1, 31, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2011, 3, 24, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2011, 4, 5, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2011, 11, 19, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2012, 1, 13, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2012, 2, 1, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2012, 11, 14, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2012, 12, 22, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2013, 1, 31, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2013, 2, 1, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2013, 12, 6, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2014, 3, 13, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2014, 3, 25, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2014, 11, 2, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2014, 11, 16, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2014, 11, 25, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2014, 11, 27, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2015, 1, 4, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2015, 2, 15, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2015, 4, 14, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2016, 1, 27, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2016, 3, 28, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2018, 1, 26, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2018, 11, 17, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2018, 11, 18, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2018, 12, 7, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2018, 12, 21, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2019, 1, 9, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2019, 2, 11, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2019, 2, 28, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2019, 2, 28, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2019, 12, 9, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2020, 1, 5, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2020, 1, 5, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2020, 1, 10, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2020, 1, 20, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2020, 1, 21, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2020, 1, 24, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2020, 2, 20, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2020, 11, 5, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2020, 11, 26, 0, 0, 0.`}, "Instant", "Gregorian", 1.`], DateObject[{2020, 12, 25, 0, 0, 0.`}, "Instant", "Gregorian", 1.`]};

To plot with DateHistogram with the option of monthly bins using DateReduction:

DateHistogram[datelist,"Month","Probability",DateReduction->"Year"]

This produces:

enter image description here

I would like to plot this data from September to May. Do you have a solution?

Thanks in advance!

$\endgroup$
4
  • 2
    $\begingroup$ Welcome to the Mathematica Stack Exchange. You mean Sept of last year to May of current year? To clear any ambiguities, could you please add datelist to your post? $\endgroup$
    – Syed
    Mar 15, 2022 at 10:09
  • $\begingroup$ Sure. I clarified my question. $\endgroup$
    – user85559
    Mar 15, 2022 at 12:03
  • 1
    $\begingroup$ Please add copy-paste-able data not an image. Thanks. $\endgroup$
    – Syed
    Mar 15, 2022 at 12:53
  • $\begingroup$ No problem. Done it. Thanks $\endgroup$
    – user85559
    Mar 15, 2022 at 14:21

2 Answers 2

6
$\begingroup$

Simple processing of the month entries in input dates gives as the desired histogram (except the bin labels):

rotatedDH = DateHistogram[MapAt[Mod[# - 8, 12, 1] &, datelist, {All, 1, 2}], 
  "Month", DateReduction -> "Year"]

enter image description here

To fix the bin labels, define a replacement rule to post-process rotatedDH:

reLabel = AssociationThread @@ Map[StringTake[#, 3] &] @ 
  {#, RotateLeft[#, 8]} & @ DateAndTime`MonthList["Gregorian"];

rotatedDH /. reLabel

enter image description here

$\endgroup$
4
$\begingroup$

An alternative approach using Counts + BarChart

shiftedShortMonthNames = RotateLeft[#, 8] & @ 
   StringTake[DateAndTime`MonthList["Gregorian"], 3]

{"Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug"}

countsByMonth = Counts[#["MonthNameShort"] & /@ datelist]

<|"Dec" -> 25, "Jan" -> 35, "Mar" -> 16, "Feb" -> 22, "Oct" -> 1, "Apr" -> 5, "Nov" -> 12|>

shiftedCounts = Join[AssociationThread[shiftedShortMonthNames, 0], countsByMonth]

<|"Sep" -> 0, "Oct" -> 1, "Nov" -> 12, "Dec" -> 25, "Jan" -> 35, "Feb" -> 22, "Mar" -> 16, "Apr" -> 5, "May" -> 0, "Jun" -> 0, "Jul" -> 0, "Aug" -> 0|>

BarChart[shiftedCounts, ChartLabels -> Automatic, BarSpacing -> 0]

enter image description here

$\endgroup$

Your Answer

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

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