4
$\begingroup$

I would appreciate help in easily converting a DateListPlot command into a DateListHistogram command. In principle, given the List, whether a plot where each point is represented by a point as versus a horizontal bar for a histogram is just a question of visual representation. I would appreciate a tip on how this can be realized in Mathematica.

$\endgroup$
2
  • $\begingroup$ Would this do the trick? DateListPlot[data, InterpolationOrder -> 0] $\endgroup$
    – chris
    Commented Oct 15, 2020 at 5:54
  • 1
    $\begingroup$ Z Ming Ma, please revisit your 18 questions and consider accepting/upvoting the answers posted. If you have not done so already please visit the page Tour to see how to vote/accept. $\endgroup$
    – kglr
    Commented Oct 15, 2020 at 8:12

2 Answers 2

5
$\begingroup$

Using data from chris's answer:

data = {{DateObject[{2016, 10, 1}, "Day", "Gregorian", -5.`], 
   10}, {DateObject[{2016, 10, 15}, "Day", "Gregorian", -5.`], 
   17}, {DateObject[{2016, 10, 30}, "Day", "Gregorian", -5.`], 
   15}, {DateObject[{2016, 11, 20}, "Day", "Gregorian", -5.`], 
   20}}; 

1. You can create a WeightedData object from data and use it with DateHistogram:

wd = WeightedData @@ Transpose[data];

DateHistogram[wd, {15, "Day"}]

enter image description here

Altenatively, specify your own bin delimiters:

binlims = Append[ wd["InputData"], 
   DatePlus[wd["InputData"][[-1]], {1, "Month"}]];

DateHistogram[wd, {binlims}]

enter image description here

2. You can use DateListStepPlot:

DateListStepPlot[data, Filling -> Axis]

enter image description here

Use the second argument to specify the steps:

Column[DateListStepPlot[data, #, Filling -> Axis, 
    PlotLabel -> Style[#, 16], ImageSize -> Large, AspectRatio -> 1/3,
     PlotRange -> {{"Sep 1, 2016", "Dec 15, 2016"}, All}] & /@
  {Center, Left, Right}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Nice! Any idea why the last bin is plotted differently in my answer? $\endgroup$
    – chris
    Commented Oct 15, 2020 at 7:27
  • $\begingroup$ @chris, you need to add a data point to the right with the same value as the last data point to get a similar picture when you use InterpolationOrder -> 0 because data is not extrapolated automatically. (I think this inconvenience is one of the reasons for introducing ListStepPlot and DateListStepPlot). $\endgroup$
    – kglr
    Commented Oct 15, 2020 at 8:05
3
$\begingroup$
data = {{DateObject[{2016, 10, 1}, "Day", "Gregorian", -5.`], 
    10}, {DateObject[{2016, 10, 15}, "Day", "Gregorian", -5.`], 
    17}, {DateObject[{2016, 10, 30}, "Day", "Gregorian", -5.`], 
    15}, {DateObject[{2016, 11, 20}, "Day", "Gregorian", -5.`], 20}}

enter image description here

DateListPlot[data, InterpolationOrder -> 0, Filling -> Bottom, 
 FillingStyle -> Pink]

enter image description here

It seems to represent differently the last day though...

$\endgroup$
3
  • $\begingroup$ I accept. DateListStepPlot produces the equivalent of the histogram I seek. Thank you very much. $\endgroup$
    – Z Ming Ma
    Commented Oct 16, 2020 at 5:59
  • $\begingroup$ ZMingMa accepting involves ticking the accept tick on the left to the answer. I would suggest you accept @kglr which is a better answer. $\endgroup$
    – chris
    Commented Oct 16, 2020 at 6:12
  • $\begingroup$ Yes. I meant to accept Suggestion 2 which is to use DateListStepPlot as a proxy for DateListHistogram. Thanks for the reminder. Z. Ming Ma $\endgroup$
    – Z Ming Ma
    Commented Oct 17, 2020 at 16:22

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.