# Plot: how to have the y-range automatically scale to min-max on the x-range?

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"]

• Probably you will be interested in How to manipulate 2D plots.
– Kuba
Commented Oct 25, 2013 at 19:56
• It is unclear to me what the problem is. Why not just PlotRange -> {{{1987, 10, 1}, {1987, 11, 30}}, All}? Commented Oct 26, 2013 at 0:04

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,
ie = Position[spx, x_ /; AbsoluteTime[end] <= AbsoluteTime[x[[1]]], {1}, 1,
Take[data, Flatten[{is, ie}]]
]

DateListPlot[iDateFilter[spx, {1987, 10, 1}, {1987, 11, 30}],
PlotLabel -> "1987"]


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.

• 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! Commented Oct 26, 2013 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) Commented Oct 26, 2013 at 13:30