python - 如何将 seaborn 图保存为 svg 或 png

大家好,我一直在尝试使用 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")

最佳答案

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

plt.savefig("coeff.png")
plt.show()

https://stackoverflow.com/questions/65896805/

相关文章:

c - 程序输出说明

powershell - 如何在 Powershell 中对 3000 万条 csv 记录进行排序

reactjs - 强制 react 功能组件触发重新渲染

node.js - 如何监视我的 azure 函数的变化?

c# - EF Core 5.0 - 更新 ASP.NET Core Web API 中的多对多实体

visual-studio-code - 更新 VS CODE 加载工作区文件夹时出错

c++ - 为什么C中总是有main函数?

c - sizeof 运算符在 c 中显示错误的结构大小

python - 获取不规则形状的中心

c++ - 具有覆盖的嵌套类如何工作?