I have a need to plot lines and a shaded region. The shaded region extends beyond the edge of the plot and I want to keep the plot limits as they were before the shaded region was plotted. I also need any autoscale changes to take into account the current axes so the limits should never shrink, and allow any lines plotted in the future to adjust the axes as they normally would had I not specified the xlim. The following code produces an image that shows what I get and what I expect. I have tried auto=True,False,None and they don't do what I expect for layered plots.
I really appreciate any help with this as I've been stuck at it for hours.
import matplotlib.pyplot as plt
import numpy as np
def shade(X, power1, power2, offset, color):
'''
This function plots a shaded region that is larger than the lines
It should not adjust the axes limits as I do not want to display anything larger than the lines
It needs to take into account previous axes limits so the axes limits never shrink
It needs to leave the axes autoscaling in a way that will allow future line plots (done by plotter) to adjust the axes limits so nothing is cut off
'''
xlims = plt.xlim() #get the xlims to be reimposed after the shaded region is plotted
xmin = min(X)
xmax = max(X)
X_enlarged = np.linspace(xmin, xmax * 2, 100)
plt.fill_between(X_enlarged, X_enlarged ** power1 + offset, X_enlarged ** power2 + offset, alpha=0.2, color=color)
#reimpose the xlims
plt.xlim(xlims, auto=None) # this line is the key to how the xlims are adjusted
def plotter(xmax, offset, color):
power1 = 2
power2 = 2.5
X1 = np.linspace(0, xmax, 100)
Y1 = X1 ** power1
Y2 = X1 ** power2
plt.plot(X1, Y1 + offset, color=color)
plt.plot(X1, Y2 + offset, color=color)
shade(X1, power1, power2, offset, color=color)
plt.subplot(231)
plotter(xmax=10, offset=0, color='red')
plt.subplot(232)
plotter(xmax=15, offset=1000, color='blue')
plt.subplot(233)
plotter(xmax=12, offset=2000, color='green')
plt.subplot(234)
plotter(xmax=10, offset=0, color='red')
plotter(xmax=15, offset=1000, color='blue')
plotter(xmax=12, offset=2000, color='green')
plt.title('What I get\nwhen combined')
plt.subplot(236)
plotter(xmax=10, offset=0, color='red')
plotter(xmax=15, offset=1000, color='blue')
plotter(xmax=12, offset=2000, color='green')
plt.xlim(-1, 16)
plt.title('What I want\nwhen combined')
plt.tight_layout()
plt.show()