2

I have a long list of {date,value}.

I would like to use DateListPlot to plot only a subrange, from date1 to date2, with the range on the y-axis automatically chosen to be between the min and max value between those two dates (i.e. trivial automatic zooming).

I am sure Mathematic will do it with a simple option choice, but I must be dumb and cannot find it.

Can you help?

Thanks.

PS: here is simple code to show the issue using financial data:

spx = FinancialData["SP500", All];

DateListPlot[spx, 
 PlotRange -> {{{1987, 10, 1}, {1987, 11, 30}}, Automatic}, 
 PlotLegends -> Placed[namesFrom83, {Left, Bottom}], 
 PlotLabel -> "1987"]
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
ac1965
  • 105
  • 6

2 Answers2

2

I rather define my own filter to get new data and put it into DateListPlot:

iDateFilter[data_, st_, end_] :=
 Block[{is, ie},
  is = Position[spx, x_ /; AbsoluteTime[x[[1]]] >= AbsoluteTime[st], {1}, 1, 
     Heads -> False];
  ie = Position[spx, x_ /; AbsoluteTime[end] <= AbsoluteTime[x[[1]]], {1}, 1, 
    Heads -> False];
  Take[data, Flatten[{is, ie}]]
 ]

DateListPlot[iDateFilter[spx, {1987, 10, 1}, {1987, 11, 30}], 
 PlotLabel -> "1987"]
halmir
  • 15,082
  • 37
  • 53
1

Using spx as defined:

With[{min = Min[spx[[;; , 2]]], max = Max[spx[[;; , 2]]]},
 DateListPlot[spx, 
  PlotRange -> {{{1987, 10, 1}, {1987, 11, 30}}, {0.9 min, 1, 1 max}},
   PlotLabel -> "1987", Joined -> True]]   

(omitting label):

Care may be needed if other datasets have missing values.

enter image description here

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
  • 1
    so you are telling me there is no simple option to tell Mathematica to do it? I am amazed, it is such a basic plotting functionality! – ac1965 Oct 26 '13 at 13:28
  • by the way, from the picture attached it seems that the second solution does not work: it is plotting the whole date range, not those 2 months in 1987 (look at the values on the y axis and you will see it immediately) – ac1965 Oct 26 '13 at 13:30