gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:37:16 24 4
gpt4 key购买 nike

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

关于python - 如何将 seaborn 图保存为 svg 或 png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65896805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com