Whenever I use imshow()
to plot an image, plotting 1D data over it in a twinned bottom x-axis changes the size and aspect ratio of the initial x-axis created with for imshow()
. How do I avoid this behavior? Here is how to reproduce the issue:
import numpy as np
import matplotlib
matplotlib.use('macosx')
import matplotlib.pyplot as plt
im = np.random.rand(2856, 4290)
light_curve = im[1000, :]
fig = plt.figure(1, figsize=(10,10))
ax1 = plt.subplot(2,1,1)
ax1.imshow(im, cmap='gray', origin='lower')
ax2 = plt.subplot(2,1,2)
ax2.imshow(im, cmap='gray', origin='lower')
# Setting aspect ratio to equal does not help
ax2.set_aspect('equal')
ax21 = ax2.twinx()
ax21.plot(light_curve, alpha=0.7)
# Setting axis limits does not help
ax1.axis([0, im.shape[1], 0, im.shape[0]])
ax21.set_xlim([0, im.shape[1]])
And here is what it looks like with my graphical backend (macosx
, if that is of any relevance)
Isn't it the purpose of twinx()
used above to help with this in the first place?
So how may I keep the initial imshow()
x-axis fixed and have the subsequent axis of the 1D plot simply fit, without resizing or messing with the aspect ratio, without going completely manual with building my axes?