4
$\begingroup$

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.

So

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

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

So I think it would be:

Manipulate[
  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?

$\endgroup$

2 Answers 2

4
$\begingroup$
L = 20; 
Manipulate[
 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

$\endgroup$
5
$\begingroup$

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};

Manipulate[
 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.

$\endgroup$
2
  • $\begingroup$ +1 I wonder whether the logo of **Weird Stuff**™ is a pentagon or an heptagon $\endgroup$ 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
    Nov 20, 2013 at 12:12

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.