# Can PlotRange of x-axis automattically determine y range?

PlotRange can specify the plotting range in each direction. Let's say you have a large set of data and you may want to plot parts of it by specifying the PlotRange of x only simply because x is the argument and although possible, sometimes you probably don't really care or want to check where this range fits in the data.

However, the y-range seems to be independent of the x-range one specifies. It's just the same as the full range when plotting all the data set, which is not ideal since a smaller x-range could mean being limited in y as well. This is shown as follows. Is there a way around to have the y-range automatically determined in a better way?

data = Table[{x, Sin[x] + Sin[1.2 x]}, {x, 0, 10 \[Pi], 0.2}];
ListLinePlot[data, PlotRange -> All, Mesh -> All]
ListLinePlot[data, PlotRange -> {{10, 25}, Automatic}, Mesh -> All]


In the latter plot, the y-range is still nearly [-2,2].

• Perhaps something like ListLinePlot[Select[data, 10 <= First@# <= 25 &], PlotRange -> {{10, 25}, Automatic}, Mesh -> All] is what must be done. I haven't yet found a purely PlotRange solution, since all the data points are in the plot generated by ListLinePlot, even when not all are shown. – Michael E2 Dec 2 '18 at 2:25
• @MichaelE2 Thanks. This is surely good and helpful. I was just wondering if it is possible within the options or so... – xiaohuamao Dec 2 '18 at 4:15
• Yes, I thought that's what you meant.,,and why I gave a fact that might indicate its impossibility. – Michael E2 Dec 2 '18 at 13:03

You need to adjust the first argument of ListLinePlot. E.g:

ListLinePlot[Select[data, Between[{10, 25}]@*First],
PlotRange -> {{10, 25}, All}, Mesh -> All]


you can use the following code. When you change xi and xf the plot range will change.

ClearAll["Global*"];
xi = 10;
xf = 25;

y[x_] := Sin[x] + Sin[1.2 x];

data = Table[y[x], {x, xi, xf, 0.1}];

minY = Min[data];
maxY = Max[data];

Plot[y[x], {x, xi, xf}, PlotRange -> {{xi, xf}, {minY, maxY}}, Mesh -> All]


The code results

• You can use MinMax@data instead of {minY, maxY} – OkkesDulgerci Dec 2 '18 at 11:40

Your problem is a nuisance for which I think there is no simple solution. To get round this I use the following code to get a point number close to the value of x I need.

ClearAll[ptNum];
ptNum[data_ /; NumberQ[data[[1]]], x_] :=
Nearest[data -> "Index", x][[1]]
ptNum[data_ /; NumberQ[data[[1, 1]]], x_] :=
Nearest[data[[All, 1]] -> "Index", x][[1]]
ptNum[data_ /; NumberQ[data[[1]]], x_List] :=
Nearest[data -> "Index", #][[1]] & /@ x
ptNum[data_ /; NumberQ[data[[1, 1]]], x_List] :=
Nearest[data[[All, 1]] -> "Index", #][[1]] & /@ x


I can then use this as follows.

{n1, n2} = ptNum[data, {10, 25}];
ListLinePlot[data[[n1 ;; n2]], Mesh -> All]


The nice thing about this approach is that you can just change the values e.g. {10,25} at one location and then press shift-enter to see the result. One can also go further with a Dynamic` scroll bar.

Hope that helps.