According to the documentation, ax.autoscale(tight=True)
should
If True, set view limits to data limits;
With ax.ax
You're not necessarily doing anything wrong. You're using matplotlib version 2 (or greater). In this version the default plot layout was changed so that the axis had 5% padding added on to either end. Here's a link describing the plot layout: https://matplotlib.org/users/dflt_style_changes.html#plot-layout
From the link, to change it back to the 'classic' style, use:
mpl.rcParams['axes.autolimit_mode'] = 'round_numbers'
mpl.rcParams['axes.xmargin'] = 0
mpl.rcParams['axes.ymargin'] = 0
By setting the autoscale
you should see the desired difference between tight=True
and tight=False
.
f, (ax, ax2) = plt.subplots(ncols=2)
ax.plot([0, 1], [1, 0], label="tight=True")
ax.autoscale(enable=True, axis='both', tight=True)
ax2.plot([0, 1], [1, 0], label="tight=False")
ax2.autoscale(enable=True, axis='both', tight=False)
ax.legend()
ax2.legend()
You may note that ax.axis("tight")
is not related; it only states in the documentation that
‘tight’ Limits set such that all data is shown
which is indeed the case, all data is shown (it doesn't say anything about setting the view limits to exactly the data).