Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted
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

share|improve this answer

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.

share|improve this answer
    
+1 I wonder whether the logo of **Weird Stuff**™ is a pentagon or an heptagon –  belisarius Nov 20 '13 at 12:09
2  
@belisarius it changes randomly between the two, and nobody can understand why. It's weird. ;-) –  Verbeia Nov 20 '13 at 12:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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