Here basically the inverse of this answer applies:
In matplotlib 2.x there is an automatic margin set at the edges, which ensures the data to be nicely fitting within the axis spines. By default it is set to 0.05 in units of axis span. This margin is not present by default in matplotlib versions prior to 2.x. However the command to set the margins is present also in previous versions:
plt.margins(x=0.04, y=0.06)
or
ax.margins(x=0.04, y=0.06)
depending on the context. Setting a single value for both axis directions is equally possible:
ax.margins(0.05)
Also see the documentation.
In case you want to set the margin in the whole script, you can use
plt.rcParams['axes.xmargin'] = 0.05
plt.rcParams['axes.ymargin'] = 0.05
at the beginning of your script (same for y of course). If you want set the margin entirely and forever, you might want to change the according line in the matplotlib rc file.
Alternatively to changing the margins, use plt.xlim(..)
or ax.set_xlim(..)
to manually set the limits of the axes such that there is no white space left.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…