I'm coming from this question Matplotlib: two x axis and two y axis where I learned how to plot two x
and y
axis on the same plot.
Here's a MWE
:
import matplotlib.pyplot as plt
import numpy as np
# Generate 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(111)
ax1.set_xlabel('x_1')
ax1.set_ylabel('y_1')
plt.plot(x1, y1, c='r')
ax2 = ax1.twinx().twiny()
ax2.set_xlabel('x_2')
ax2.set_ylabel('y_2')
plt.ylim(min(y2), max(y2))
ax2.plot(x2, y2, c='b')
plt.show()
and this is the output:
The right y
axis and the top x
axis correspond to the blue curve.
As you can see, the second y
label is missing even though it is defined. I've tried a number of different approaches but I can't get it to show. Am I doing something wrong?
Add:
apparently there's an issue with the line:
ax2 = ax1.twinx().twiny()
if I invert it like so:
ax2 = ax1.twiny().twinx()
then it's the second x
label that will not show.