Wolfram Research

Function Repository Resource:

SuggestPlotRange

Source Notebook

Get a variable range against which to plot a given function

Contributed by: Paco Jain (Wolfram Research)

ResourceFunction["SuggestPlotRange"][expr,x]

tries to return a range in x that will produce a nice-looking plot of the expression expr versus x.

Details and Options

ResourceFunction["SuggestPlotRange"][expr, x] returns a result of the form {x, xmin, xmax} that can be used directly as a range specifier in Plot.
SuggestPlotRange takes the following options:
"Periods" Automatic how many periods of a periodic function to plot
"Range" "Narrow" whether to give a "Narrow" or "Wide" plot range

Examples

Basic Examples

Suggest a plot range for a trigonometric function:

In[1]:=
func = Sin[20 x];
ResourceFunction["SuggestPlotRange"][func, x]
Out[2]=
In[3]:=
Plot[func, %]
Out[3]=

Suggest a plot range for a polynomial function:

In[4]:=
func = (x - 1) (x - 2) (x - 3) (x - 3.2);
ResourceFunction["SuggestPlotRange"][func, x]
Out[5]=
In[6]:=
Plot[func, %]
Out[6]=

Suggest a plot range for a rational function:

In[7]:=
func = (x^2 + x + 1)/(x + 1);
ResourceFunction["SuggestPlotRange"][func, x]
Out[8]=
In[9]:=
Plot[func, %]
Out[9]=

Suggest a plot range for a generic function:

In[10]:=
func = Sin[x]^3;
ResourceFunction["SuggestPlotRange"][func, x]
Out[11]=
In[12]:=
Plot[func, %]
Out[12]=

Suggest a plot range for another function:

In[13]:=
func = (x^3 + 1)^(1/3);
ResourceFunction["SuggestPlotRange"][func, x]
Out[14]=
In[15]:=
Plot[func, %]
Out[15]=

Options

Using the option setting Range "Wide" will give a range showing more of the function but possibly revealing less detail:

In[16]:=
func = (x^5 + x^9 - x - 1)^3;
ResourceFunction["SuggestPlotRange"][func, x]
Out[17]=
In[18]:=
Plot[func, %]
Out[18]=
In[19]:=
ResourceFunction["SuggestPlotRange"][func, x, "Range" -> "Wide"]
Out[19]=
In[20]:=
Plot[func, %]
Out[20]=

For periodic functions, the option setting "Periods"n will return a range covering the specified number of periods:

In[21]:=
func = Sin[20 x];
ResourceFunction["SuggestPlotRange"][func, x, "Periods" -> 3]
Out[22]=
In[23]:=
Plot[func, %]
Out[23]=

Resource History

License Information