I constructed a table (here called 'distanceplot') with a set domain (from 0 to 100). I wish to keep this domain for the table constant. However, I shall be making multiple plots, each using a different subset of the domain. For example, I shall be making a plot which used only data from 0 to 10. Is there a way for ListPlot to restrict the domain already given to a table? I attempted to use the command DataRange, but it didn't change anything.

I tried the following


I know how to do this for Plot, but ListPlot I am not sure.

  • 1
    $\begingroup$ PlotRange -> {{0, 10}, Automatic} ? What do you use in the case of Plot, and how does it fail? $\endgroup$
    – MarcoB
    Jul 31, 2015 at 19:48

1 Answer 1


It may depend on what you would like to achieve visually with your plot.

I'll create some fake data to represent yours:

distanceplot = Table[{x, 2 x}, {x, 0, 100, 1}];

Here are two ways to achieve what you asked, which lead to somewhat different results

The first example, which I suggested in the comments, provides an explicit limit for the horizontal plot range. The vertical plot range is determined automatically by ListPlot, taking into consideration all your data:

ListPlot[distanceplot, PlotRange -> {{0, 10}, Automatic}, PlotRangePadding -> Scaled[0.05]]

Mathematica graphics

As you can see, the vertical range goes all the way to 200, because that is the range encompassed by the full dataset. If this behavior is undesirable, you could a) give an explicit value for the $y$ range as well, or b) pre-select the data that you feed to ListPlot with e.g. Cases:

ListPlot[Cases[distanceplot, {x_, y_} /; 0 <= x <= 10], PlotRangePadding -> Scaled[0.05]]

Mathematica graphics

In this case, we don't need to fiddle with the PlotRange at all: we simply feed only the data to be plotted to ListPlot, and let it do its internal magic to determine the appropriate PlotRange for this subset.

Note that, using this approach, you can filter your data any way you want. Suppose that you wanted only those points $10\le x \le 45$ for which $x$ is a prime number; you could then write:

 Cases[distanceplot, {x_, y_} /; 10 <= x <= 45 && PrimeQ[x]],
 PlotRangePadding -> Scaled[0.05], AxesOrigin -> {0, 0}

Mathematica graphics

  • $\begingroup$ Thank you very much, that was exactly what I was looking for. $\endgroup$
    – zalba19
    Aug 4, 2015 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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