Let's say I have a one dimension list d with length L. The $x$-axis in the plot starts from {2013, 8, 28 + start} and ends in {2013, 8, 28 + end}, and I want to change the start and end date with Manipulate.


d[[1]] (*--> 2013/8/28*)

d[[2]] (*--> 2013/8/29*)

So I think it would be:

  DateListPlot[d, {{2013, 8, 28 + start}, {2013, 8, 28 + end}}],
  {{start, 0}, 0, L},
  {{end, L}, 0, L}

But when I plot it, it gives me an error when a>3 because the day of the date is greater than 31 (I think).

How would be the correct way to do this?


2 Answers 2

L = 20; 
 DateListPlot[Range@50, {DatePlus[{2013, 8, 28}, start], DatePlus[{2013, 8, 28}, end]}],
 {{start, 0}, 0, L},
 Item[Dynamic@DatePlus[{2013, 8, 28}, start], Alignment -> Left],
 {{end, L}, 0, L},
 Item[Dynamic@DatePlus[{2013, 8, 28}, end], Alignment -> Left]]

Mathematica graphics


If you do not know the frequency of your data, or just want to be robust to it, there are two options. One is the standard Take or Part constructs. The other is PlotRange, which works fine for the horizontal axis of DateListPlots.

fakedata = 
  Transpose@{DatePlus[{2001, 1}, {#, "Month"}] & /@ Range[0, 99], 
    Accumulate[RandomVariate[NormalDistribution[0, 1], {100}]] - 2};

 DateListPlot[fakedata, Joined -> True, 
  PlotRange -> {{start, end}, Automatic}], 
 {start, AbsoluteTime@{2000, 12}, AbsoluteTime@{2004, 6}}, 
 {end, AbsoluteTime@{2005, 6}, AbsoluteTime@{2009}}]

enter image description here

You could change the outer bounds of the Manipulate ranges for start and end to reference the data if you wanted. Just be careful to make sure that start cannot be greater than end, or Weird Stuff™ happens.

  • $\begingroup$ +1 I wonder whether the logo of **Weird Stuff**™ is a pentagon or an heptagon $\endgroup$ Commented Nov 20, 2013 at 12:09
  • 2
    $\begingroup$ @belisarius it changes randomly between the two, and nobody can understand why. It's weird. ;-) $\endgroup$
    – Verbeia
    Commented Nov 20, 2013 at 12:12

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.