如何将seaborn图保存为svg或png

DDM*_*DDM 5 python python-3.x seaborn

大家好,我一直在尝试将输出图表保存为plt.savefig("coeff.png")svg或 png,但我得到的只是一张空白图片。

无论如何,我可以将我的绘图导出为图片格式吗?

下面是我的代码。

# These are the (standardized) coefficients found
# when it refit using that best alpha
list(zip(X.columns, LGBMR.feature_importances_))

#Now let's make it more presentable in Pandas DataFrame and also in standard form for numbers
df_LGBM_model_coefficients = pd.DataFrame(list(zip(X_train.columns, LGBMR.feature_importances_)))
df_LGBM_model_coefficients.rename(columns={0:'Features', 1: 'Coefficients'}, inplace=True)

df_LGBM_model_coefficients = df_LGBM_model_coefficients.iloc[1:]
df_LGBM_model_coefficients = df_LGBM_model_coefficients.sort_values(by = 'Coefficients', ascending = False)

#only retain the important ones.
df_LGBM_model_coefficients_top_10 = df_LGBM_model_coefficients.head(10)



plt.figure(figsize=(12,12))
sns.set_style('whitegrid')
sns.set(font_scale=0.8)

ax = sns.barplot(x = 'Coefficients',y='Features', data = df_LGBM_model_coefficients_top_10)
ax.set_title("LGBMR Top 10 Features and their coefficients")

for p in ax.patches:
    ax.annotate("%.1f" % p.get_width(), (p.get_x() + p.get_width(), p.get_y() + 0.8), xytext=(5, 10), textcoords='offset pixels')
plt.show()
plt.savefig("coeff.png")
Run Code Online (Sandbox Code Playgroud)

ash*_*l16 13

交换代码的最后两行。将线放在线plt.savefig("coeff.png")之前plt.show()

plt.savefig("coeff.png")
plt.show()
Run Code Online (Sandbox Code Playgroud)