0
$\begingroup$

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

ListPlot[distanceplot,DataRange->{0,10}]

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

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

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:

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

Mathematica graphics

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

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.