You can avoid this with the command plt.autoscale(False)
after the first plot.
Also, in my opinion, it would be a better solution to use plt.axhline
rather than plt.plot
to make a horizontal line, as the axhline
spans the whole horizontal extent of the axes, even if you pan/zoom in the plot.
In other words, your example could be rewritten like this:
import scipy.stats as stat
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
data = stat.uniform.rvs(size=2400).reshape((40,60))
plt.figure(figsize=(12, 4))
plt.axes([.05,.1,.4,.8])
plt.imshow(data,cmap=cm.jet,vmin=0,vmax=1)
plt.colorbar(fraction=.03)
plt.autoscale(False)
plt.axhline(y=20, c='w', lw=3)
plt.title('the damn white line')