边缘处设置了自动边距,确保数据很好地贴合在轴脊内。在这种情况下,y 轴上可能需要这样的边距。默认情况下它设置为0.05
以轴跨度为单位。
将边距设置为0
在 x 轴上,使用
plt.margins(x=0)
or
ax.margins(x=0)
取决于上下文。另请参阅文档 http://matplotlib.org/devdocs/api/_as_gen/matplotlib.axes.Axes.margins.html.
如果你想去掉整个脚本中的边距,你可以使用
plt.rcParams['axes.xmargin'] = 0
在脚本的开头(与y
当然)。如果您想完全永久地消除边距,您可能需要更改中的相应行matplotlib rc 文件 http://matplotlib.org/users/customizing.html:
axes.xmargin : 0
axes.ymargin : 0
Example
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')
ax2.margins(x=0)
或者,使用plt.xlim(..) https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.xlim.html or ax.set_xlim(..) https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set_xlim.html手动设置轴的限制,以便不留空白。