3.14 Setting axis ranges

By default, Pyxplot automatically scales axes to some sensible range which contains all of the plotted data. However, it is possible for the user to override this and set his own range. This can be done directly from the plot command, by following the word plot with the syntax [minimum:maximum].1 The first specified range applies to the x-axis, and the second to the y-axis.2 In the following example, the first three cylindrical Bessel functions are plotted in the range $0<x<10$:

plot [0:10][-0.5:1] besselJ(0,x), besselJ(1,x), besselJ(2,x)
\includegraphics[width=8cm]{examples/eps/ex_intro_bessel}

Any of the values can be omitted, as in the following plot of three Legendre polynomials:

set key xcenter
plot [-1:1][:] legendreP(2,x), legendreP(4,x), legendreP(6,x)
\includegraphics[width=8cm]{examples/eps/ex_intro_legendre}

Here, we have used the set key command to specify that the plot’s legend should be horizontally aligned in the center of the plot, to complement the symmetry of the Legendre polynomials. This command will be described more fully in Section 8.7.

Alternatively, ranges can be set before the plot statement, using the set xrange command, as in the examples:

set xrange [-2:2]
set yrange [a:b]

If an asterisk is supplied in place of either of the limits in this command, then any limit which had previously been set is switched off, and the axis returns to its default autoscaling behaviour:

set xrange [-2:*]

A similar effect may be obtained using the set autoscale command, which takes a list of the axes to which it is to apply. Both the upper and lower limits of these axes are set to scale automatically. If no list is supplied, then the command is applied to all axes.

set autoscale x y
set autoscale

The range supplied to the set xrange can be followed by the word reverse to indicate that the axis should run from right-to-left, or from top-to-bottom. In practice, this is of limited use when an explicit range is specified, as the following two commands are equivalent:

set xrange [-2:2] reverse
set xrange [2:-2] noreverse

However, this is useful when axes are set to autoscale:

set xrange [*:*] reverse

Axes can be set to have logarithmic scales by using the set logscale command, which also takes a list of axes to which it should apply. Its converse is set nologscale:

set logscale
set nologscale y x x2

Further discussion of the configuration of axes can be found in Section 8.8.1.


Example: A diagram of the trajectories of projectiles fired with different initial velocities

In this example we produce a diagram of the trajectories of projectiles fired by a cannon at the origin with different initial velocities $v$ and different angles of inclination $\theta $. According to the equations of motion under constant acceleration, the distance of such a projectile from the origin after time $t$ is given by
  $\displaystyle  x(t)  $ $\displaystyle  =  $ $\displaystyle  vt\cos \, \theta  $    
  $\displaystyle h(t)  $ $\displaystyle  =  $ $\displaystyle  vt\sin \, \theta + \nicefrac {1}{2}gt^2  $    
where $x(t)$ is the horizontal displacement of the projectile and $h(t)$ the vertical displacement. Eliminating $t$ from these equation gives the expression
  \[  h(x) = x\tan \, \theta - \frac{gx^2}{2v^2\cos ^2\, \theta }.  \]    

In the script below, we plot this function for five different values of $v$ and $\theta $.
g   = phy.g     # Acceleration due to gravity
deg = unit(deg) # Convert degrees to radians

# The mathematical equation of a trajectory
h(x,theta,v) = x*tan(theta*deg) - 0.5*g*x**2/(v**2*cos(theta*deg)**2)

# Plot configuration
set xlabel r"$x$"
set ylabel r"$h$"
set xrange [unit(0*m):unit(20*m)]
set yrange [unit(0*m):]
set key below
set title r’Trajectories of projectiles fired with speed $v$ at angle $$\backslash $theta$’
plot h(x,30,unit(10*m/s)) t r"$$\backslash $theta=30\^{}$\backslash $circ;$\backslash $qquad v=10$\backslash $,{$\backslash $rm m$\backslash $,s\^{}{-1}}$", $\backslash $
     h(x,45,unit(10*m/s)) t r"$$\backslash $theta=45\^{}$\backslash $circ;$\backslash $qquad v=10$\backslash $,{$\backslash $rm m$\backslash $,s\^{}{-1}}$", $\backslash $
     h(x,60,unit(10*m/s)) t r"$$\backslash $theta=60\^{}$\backslash $circ;$\backslash $qquad v=10$\backslash $,{$\backslash $rm m$\backslash $,s\^{}{-1}}$", $\backslash $
     h(x,30,unit(15*m/s)) t r"$$\backslash $theta=30\^{}$\backslash $circ;$\backslash $qquad v=15$\backslash $,{$\backslash $rm m$\backslash $,s\^{}{-1}}$", $\backslash $
     h(x,60,unit(15*m/s)) t r"$$\backslash $theta=60\^{}$\backslash $circ;$\backslash $qquad v=15$\backslash $,{$\backslash $rm m$\backslash $,s\^{}{-1}}$"

\includegraphics{examples/eps/ex_trajectories}


Footnotes

  1. An alternative valid syntax is to replace the colon with the word to, i.e. [minimum to maximum].
  2. As will be discussed in Section 8.8.1, if further ranges are specified, they apply to the x2-axis, then the y2-axis, and so forth.