在matplotlib画图时,文字等图形元素有可能会超出图片边框,导致图片显示不全
比如以下代码:
sns.lmplot(x='Sepal_Length', y='Petal_Width', data=data, hue="Species", fit_reg=False)
plt.title("鸢尾花种类分布图")
plt.show()
输入图片结果为:
可以看到title与横轴名称都不在图片内
解决方法:
使用tight_layout
sns.lmplot(x='Sepal_Width', y='Petal_Length', data=data, hue="Species", fit_reg=False)
plt.title("鸢尾花种类分布图")
plt.tight_layout()
plt.show()
也可以使用plt.subplots(constrained_layout=True)