Use logarithmic scale axes in Matplotlib

Python

Matplotlib

Axes

Use log scale

Luc B.

Python

Matplotlib

Logarithmic scale axes are crucial for visualizing data that spans a wide range of values. Fortunately, Matplotlib makes it incredibly easy to change axis scales.

Code Example

Use the plt.yscale() and plt.xscale() functions to change the axis scales.

import matplotlib.pyplot as plt
import numpy as np

x_values = np.linspace(0, 10)
y_values = np.exp(x_values)
plt.plot(x_values, y_values)

# Make the y-axis logarithmic here
plt.yscale('log')

plt.show()

png

More Examples

Object Oriented Interface

Matplotlib's object oriented interface implements the Axes.set_xscale() and Axes.set_yscale() functions to make axes logarithmic.

ax = plt.axes()

ax.plot(x_values, y_values)

# Make the x-axis logarithmic here
ax.set_xscale('log')

plt.show()

png