Set the axis limits in Matplotlib

Python

Matplotlib

Axes

Set limits

Luc B.

Python

Matplotlib

While Matplotlib is good at choosing axis limits that suit your visualization, it is often useful to adjust them manually. Setting the axis limits allows focusing attention on important elements of the data and hiding less relevant ones.

Code Example

Use the plt.xlim() and plt.ylim() functions to set the axis limits in Matplotlib. These functions take two numerical arguments: the lower limit followed by the upper limit.

import matplotlib.pyplot as plt

# Set axis limits here
plt.xlim(0, 100)
plt.ylim(0, 0.01)

plt.show()

png

More Examples

Object Oriented Interface

Matplotlib's object oriented interface implements the Axes.set_xlim() and Axes.set_ylim() functions for customizing the axis limits.

ax = plt.axes()

# Set axis limits here
ax.set_xlim(0, 100)
ax.set_ylim(0, 0.01)

plt.show()

png