无法使用长 yticklabel 保存条形图的整个图形

Could not save whole figure of barplot with long yticklabel

我想保存条形图,但在保存到文件时发现它被裁剪了。

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")
sns.set_theme(font_scale=1.6)
fig,ax = plt.subplots(figsize=(8,6))
g = sns.barplot(x="tip", y="day", data=tips)
g.set(yticklabels=['Thur','Fri','Sat','Very long long long long Sun'])
fig.savefig('1.png',dpi=400)

这里是jupyter notebook中显示的图

然而,保存的图形是这样的:

您应该将 bbox_index='tight' 添加为 plt.savefig()

的参数和自变量
fig.savefig('1.png',dpi=400, bbox_inches='tight')