Setting an axis range
By default, matplotlib will find the minimum and maximum of your data on both axes and use this as the range to plot your data. However, it is sometimes preferable to manually set this range, to get a better view of the data's extrema. In this recipe, we are going to see how to set an axis range.
How to do it...
The pyplot
API provides a function to directly set the range of one axis, as follows:
import numpy as np import matplotlib.pyplot as plt X = np.linspace(-6, 6, 1024) plt.ylim(-.5, 1.5) plt.plot(X, np.sinc(X), c = 'k') plt.show()
The preceding script draws a curve. In contrast with the default settings, the graphic does not fit the curve perfectly; we have some room at the upper part of the curve, as shown in the following figure:
How it works...
The pyplot.xlim()
and pyplot.ylim()
parameters allow us to control the range of the x axis and y axis respectively. These parameters are the maximum and minimum values.