Matplotlib - Twin Axes

It is considered useful to have dual x or y axes in a figure. Moreso, when plotting curves with different units together. Matplotlib supports this with the twinxand twiny functions.

In the following example, the plot has dual y axes, one showing exp(x) and the other showing log(x) −

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
a1 = fig.add_axes([0,0,1,1])
x = np.arange(1,11)
a2 = a1.twinx()
a2.plot(x, np.log(x),'ro-')
fig.legend(labels = ('exp','log'),loc='upper left')
Twin Axes
Kickstart Your Career

Get certified by completing the course

Get Started