How does Plot determine vertical range when not user-specified? - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-18T06:32:57Z https://mathematica.stackexchange.com/feeds/question/30982 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/30982 7 How does Plot determine vertical range when not user-specified? kjo https://mathematica.stackexchange.com/users/2464 2013-08-23T13:18:15Z 2013-08-23T18:25:23Z <p>Consider the example<sup>1</sup>:</p> <pre><code>p0 = Plot[Tan[x Pi/2], {x, -1, 1}] </code></pre> <p>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 <code>AbsoluteOptions[p0, PlotRange]</code>).</p> <p>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</p> <pre><code>epsilon = 1*^-6; Plot[Tan[x Pi/2], {x, -1 + epsilon, 1 - epsilon}] </code></pre> <p>the vertical plot range used by Mathematica is $[-6.60183, 6.50749]$, almost the same as the one used in the previous example.</p> <p>How does <code>Plot</code> decide the vertical range of a 2D plot, when the user does not specify (a bounded) one?</p> <p>(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.)</p> <hr> <p>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</p> <pre><code>Plot[Tan[x Pi/200], {x, -100, 100}] </code></pre> <p>is exactly the same as for the first example above.</p> <p><sub><sup>1</sup> Sorry for not posting pictures... The SE Uploader I have installed is not working for some reason.</sub></p> https://mathematica.stackexchange.com/questions/30982/-/31001#31001 5 Answer by bill s for How does Plot determine vertical range when not user-specified? bill s https://mathematica.stackexchange.com/users/1783 2013-08-23T18:25:23Z 2013-08-23T18:25:23Z <p>It's not just <code>Plot</code> -- <code>ListPlot</code> 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:</p> <pre><code>x = RandomVariate[UniformDistribution[{-1, 1}], 1000]; ListPlot[x] </code></pre> <p><img src="https://i.stack.imgur.com/DjyeV.png" alt="enter image description here"></p> <p>Change two of the values to +/-2 and it also acts as you might expect:</p> <pre><code>x = RandomVariate[UniformDistribution[{-1, 1}], 1000]; x[] = 2; x[] = -2; ListPlot[x] </code></pre> <p><img src="https://i.stack.imgur.com/CFBIZ.png" alt="enter image description here"></p> <p>But change the values to +/-200 and it makes some choices about what is the right thing to plot:</p> <pre><code>x = RandomVariate[UniformDistribution[{-1, 1}], 1000]; x[] = 200; x[] = -200; ListPlot[x] </code></pre> <p><img src="https://i.stack.imgur.com/lFGEV.png" alt="enter image description here"></p> <p>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 <code>Plot</code> as well as many of the other plotting functions):</p> <pre><code>x = RandomVariate[UniformDistribution[{-1, 1}], 1000]; x[] = 200; x[] = -200; ListPlot[x, PlotRange -&gt; All] </code></pre> <p><img src="https://i.stack.imgur.com/bNVRT.png" alt="enter image description here"></p>