Make matplotlib autoscaling ignore some of the plots

Use the scalex/scaley kw arg:

plot(x1, 3*sin(x1), scaley=False)

The obvious way is to just manually set the limits to what you want. (e.g. ax.axis([xmin, xmax, ymin, ymax]))

If you don't want to bother with finding out the limits manually, you have a couple of options...

As several people (tillsten, Yann, and Vorticity) have mentioned, if you can plot the function you want to ignore last, then you can disable autoscaling before plotting it or pass the scaley=False kwarg to plot

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
x1 = np.linspace(-1,1,100)

ax.plot(x1, np.sin(x1))
ax.plot(x1, np.sin(x1 / 2.0))
ax.autoscale(False)         #You could skip this line and use scalex=False on
ax.plot(x1, 3 * np.sin(x1)) #the "theoretical" plot. It has to be last either way

fig.savefig('test.pdf')

Note that you can adjust the zorder of the last plot so that it's drawn in the "middle", if you want control over that.

If you don't want to depend on the order, and you do want to just specify a list of lines to autoscale based on, then you could do something like this: (Note: This is a simplified version assuming you're dealing with Line2D objects, rather than matplotlib artists in general.)

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms

def main():
    fig, ax = plt.subplots()
    x1 = np.linspace(-1,1,100)

    line1, = ax.plot(x1, np.sin(x1))
    line2, = ax.plot(x1, 3 * np.sin(x1))
    line3, = ax.plot(x1, np.sin(x1 / 2.0))
    autoscale_based_on(ax, [line1, line3])

    plt.show()

def autoscale_based_on(ax, lines):
    ax.dataLim = mtransforms.Bbox.unit()
    for line in lines:
        xy = np.vstack(line.get_data()).T
        ax.dataLim.update_from_data_xy(xy, ignore=False)
    ax.autoscale_view()

if __name__ == '__main__':
    main()

enter image description here


This can be done regardless of plotting order by creating another axes to work on.

In this version, we create a twin axes and disable the autoscaling on that twin axes. In this way, the plot is scaled based on anything plotted in the original axes, but is not scaled by anything put into the twin axes.

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
x1 = np.linspace(-1,1,100)
twin_ax = ax.twinx()  # Create a twin axes.
twin_ax.autoscale(False)  # Turn off autoscaling on the twin axes.
twin_ax.set_yticks([])  # Remove the extra tick numbers from the twin axis.

ax.plot(x1, np.sin(x1))
twin_ax.plot(x1, 3 * np.sin(x1), c='green')  # Plotting the thing we don't want to scale on in the twin axes.
ax.plot(x1, np.sin(x1 / 2.0))

twin_ax.set_ylim(ax.get_ylim())  # Make sure the y limits of the twin matches the autoscaled of the original.

fig.savefig('test.pdf')

Not scaled twin axes

Note, the above only prevents the un-twined axis from auto scaling (y in the above case). To get it to work for both x and y, we can do the twinning process for both x and y (or create the new axes from scratch):

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
x1 = np.linspace(-1,1,100)
x2 = np.linspace(-2,2,100)  # Would extend the x limits if auto scaled
twin_ax = ax.twinx().twiny()  # Create a twin axes.
twin_ax.autoscale(False)  # Turn off autoscaling on the twin axes.
twin_ax.set_yticks([])  # Remove the extra tick numbers from the twin axis.
twin_ax.set_xticks([])  # Remove the extra tick numbers from the twin axis.

ax.plot(x1, np.sin(x1))
twin_ax.plot(x2, 3 * np.sin(x2), c='green')  # Plotting the thing we don't want to scale on in the twin axes.
ax.plot(x1, np.sin(x1 / 2.0))

twin_ax.set_ylim(ax.get_ylim())  # Make sure the y limits of the twin matches the autoscaled of the original.
twin_ax.set_xlim(ax.get_xlim())  # Make sure the x limits of the twin matches the autoscaled of the original.

fig.savefig('test.png')

LineCollection objects can be ignored by using the autolim=False argument:

from matplotlib.collections import LineCollection

fig, ax = plt.subplots()
x1 = np.linspace(-1,1,100)

# Will update limits
ax.plot(x1, np.sin(x1))

# Will not update limits
col = LineCollection([np.column_stack((x1, 3 * np.sin(x1)))], colors='g')
ax.add_collection(col, autolim=False)

# Will still update limits
ax.plot(x1, np.sin(x1 / 2.0))

See this image of the output