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

enter image description here enter image description here

  • 2
    $\begingroup$ 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. $\endgroup$ – Michael E2 Dec 2 '18 at 2:25
  • $\begingroup$ @MichaelE2 Thanks. This is surely good and helpful. I was just wondering if it is possible within the options or so... $\endgroup$ – xiaohuamao Dec 2 '18 at 4:15
  • $\begingroup$ Yes, I thought that's what you meant.,,and why I gave a fact that might indicate its impossibility. $\endgroup$ – 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]

| improve this answer | |

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

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

enter image description here

| improve this answer | |
  • $\begingroup$ You can use MinMax@data instead of {minY, maxY} $\endgroup$ – 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.

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]

Mathematica graphics

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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