I'm trying to plot a graph that has two x-axes on matplotlib. However, it's not behaving how I would expect it to
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax1 = fig.add_subplot(111)
x = np.linspace(-10,10,1000)
y = np.sin(x*np.pi/2)
ax1.plot(x,y)
ax2 = ax1.twiny()
tick_locations = ax1.get_xticks() - 5.
tick_labels = ax1.get_xticks()
ax2.set_xticks(tick_locations[1:])
ax2.set_xticklabels(tick_labels[1:])
plt.show()
gives:
Shouldn't the ax2 labels read: [ -5. 0. 5. 10.]
at ax1 locations [-10. -5. 0. 5.]
Any ideas why it's stretching them out?