Cancelling the offset on an axis [duplicate]

In matplotlib v2.0.x, the default axes margin has changed, from 0 to 0.05, which is the value controlling the whitespace around your data on the axes. See here for more on the reasoning behind this change.

There are several ways to revert to the previous behaviour.

1) To reset margins to 0 for a single Axes instance:

plt.margins(0)

or

ax.margins(0)

2) To reset margins to 0 for all plots in a script, use rcParams and set this at the top of your script:

plt.rcParams['axes.autolimit_mode'] = 'round_numbers'
plt.rcParams['axes.xmargin'] = 0.
plt.rcParams['axes.ymargin'] = 0.

3) To change the default value for all plots on a machine, modify your the matplotlibrc file to include these lines:

axes.autolimit_mode: round_numbers
axes.xmargin        : 0.
axes.ymargin        : 0.

Note that to use method (1) and truly get the old behaviour, you may also need to set plt.rcParams['axes.autolimit_mode'] = 'round_numbers'.

Leave a Comment