I've seen several question asking how to add a secondary x or y axis but none asking if adding both at the same time is possible.
I know ax.twinx()
and ax.twiny()
are used to add either another y
or x
axis respectively, but how would one go about adding both?
Here's an example of two curves with different x,y ranges:
import matplotlib.pyplot as plt
import numpy as np
# Generate two sets of random data.
x1 = np.random.randn(50)
y1 = np.linspace(0, 1, 50)
x2 = np.random.randn(20)+15.
y2 = np.linspace(10, 20, 20)
# Plot both curves.
fig = plt.figure()
ax1 = fig.add_subplot(121)
plt.plot(x1, y1, c='r')
ax2 = fig.add_subplot(122)
plt.plot(x2, y2, c='b')
plt.show()
This results in:
and what I'm after is something like this:
where the right y
axis and the top x
axis correspond to the blue curve.
Can this be done?