
Low, high = calculate_new_limit(fixed, dependent, lim) With margins in fraction of the width of the plotĭefaults to current axes object if not specified. To fit the margins set by manually limits of the other axis, '''Autoscales the x or y axis of a given matplotlib ax object def autoscale(ax=None, axis='y', margin=0.1): If you wish to edit it, please fork it on gist. It can be called as simple as autoscale() to work on the most recent axes. I've built upon answer to cover cases of plot, scatter and axhline/axvline for scaling either the x or y axis. Here is a simple example using this function: import numpy as npįig,axs = plt.subplots(1,2,figsize=(8,5))Īxs.set_title('Rescaled x-axis\nand used "autoscale_y"')

This is something of a hack, and will probably not work in many situations, but for a simple plot, it works well. Y_displayed = yd[((xd>lo) & (xd top: top = new_top Margin - the fraction of the total height of the y-data to pad the upper and lower ylims""" """This function rescales the y-axis based on the data that is visible given the current xlim of the axis. Here is a function that just rescales the y-axis based on the data that is in the visible x-region: def autoscale_y(ax,margin=0.1):

Additionally, it would be nice to have an "autoscale_y" function that only requires the axes object (i.e., unlike the answer here, which requires direct use of the data.) While Joe Kington certainly proposes the most sensible answer when he recommends that only the necessary data be plotted, there are situations where it would be best to plot all of the data and just zoom to a certain section.
