How can I change the x axis so there is no white space?

There is an automatic margin set at the edges, which ensures the data to be nicely fitting within the axis spines. In this case such a margin is probably desired on the y axis. By default it is set to 0.05 in units of axis span.

To set the margin to 0 on the x axis, use




depending on the context. Also see the documentation.

In case you want to get rid of the margin in the whole script, you can use

plt.rcParams['axes.xmargin'] = 0

at the beginning of your script (same for y of course). If you want to get rid of the margin entirely and forever, you might want to change the according line in the matplotlib rc file:

axes.xmargin : 0
axes.ymargin : 0


import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset('tips')

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
tips.plot(ax=ax1, title="Default Margin")
tips.plot(ax=ax2, title="Margins: x=0")

enter image description here

Alternatively, use plt.xlim(..) or ax.set_xlim(..) to manually set the limits of the axes such that there is no white space left.

Leave a Comment