# How does Plot determine vertical range when not user-specified?

Consider the example1:

p0 = Plot[Tan[x Pi/2], {x, -1, 1}]


In this case, the vertical range specified would be $(-\infty, \infty)$, which is clearly impossible to plot, but Mathematica silently chooses a vertical plot range of $[-6.60184, 6.50751]$ (as reported by AbsoluteOptions[p0, PlotRange]).

In fact, even in cases where the function to be plotted remains finite throughout the interval specified (including its endpoints), Mathematica will sometimes choose a vertical range smaller than the actual range of the function in the specified interval. For example, if one plots

epsilon = 1*^-6;
Plot[Tan[x Pi/2], {x, -1 + epsilon, 1 - epsilon}]


the vertical plot range used by Mathematica is $[-6.60183, 6.50749]$, almost the same as the one used in the previous example.

How does Plot decide the vertical range of a 2D plot, when the user does not specify (a bounded) one?

(Needless to say, I don't expect to get a full description of Mathematica's algorithm for doing this, since it's proprietary information. What I'm hoping to get is just the general idea, in broad strokes.)

One thing I can say is that the horizontal range does not seem to enter into the computation of the vertical range, since the vertical range for the plot

Plot[Tan[x Pi/200], {x, -100, 100}]


is exactly the same as for the first example above.

1 Sorry for not posting pictures... The SE Uploader I have installed is not working for some reason.

• It knows the function, since you are passing that to it, it knows it range, hence it will do some preprocessing to estimate the maximum/minimum of the function(s) to be plotted over the range. There is, I am sure lots of additional heuristics involved in order to arrive at values that make the plot looks best. It is both an art and science. Mathematica does this better than Matlab for example. This is an educated guess, since I do not work for WRI and do not know how this is done in actual implementation. Commented Aug 23, 2013 at 13:27
• @Nasser Plot does not use any symbolic processing for this because the result is always the same irrespective of whether the argument function is "black-box" or explicitly visible for Plot. Compare: f[x_?NumberQ] = Tan[x Pi/200]; Plot[f[x], {x, -100, 100}] and Plot[Tan[x Pi/200], {x, -100, 100}]. Commented Aug 23, 2013 at 14:42
– rm -rf
Commented Aug 23, 2013 at 19:03
• @rm-rf: thanks; as it happens, that is the uploader that I'm using; it is wedged, somehow; I need to reinstall it, but this is not the first time that this happens, which makes me reluctant to repeat the cycle until I understand why it gets wedged in the first place...
– kjo
Commented Aug 23, 2013 at 22:38

It's not just Plot -- ListPlot acts similarly, and it's quite easy to explore. For example, if you have a bunch of numbers between -1 and +1, it displays the range you might expect:

x = RandomVariate[UniformDistribution[{-1, 1}], 1000];
ListPlot[x]


Change two of the values to +/-2 and it also acts as you might expect:

x = RandomVariate[UniformDistribution[{-1, 1}], 1000];
x[[100]] = 2; x[[200]] = -2;
ListPlot[x]


But change the values to +/-200 and it makes some choices about what is the right thing to plot:

x = RandomVariate[UniformDistribution[{-1, 1}], 1000];
x[[100]] = 200; x[[200]] = -200;
ListPlot[x]


Notice that it does not just truncate the values (and plot as in the top picture), rather, it tries to indicate that there is more to the plot than might be apparent by choosing a range of about +/-2.5. Only Wolfram knows why it makes these exact choices. If you want to bypass this, you can always use the option to include all the points (which works for Plot as well as many of the other plotting functions):

x = RandomVariate[UniformDistribution[{-1, 1}], 1000];
x[[100]] = 200; x[[200]] = -200;
ListPlot[x, PlotRange -> All]