【问题标题】:The seaborn not saving the whole figure but only part of itseaborn 没有保存整个数字,但只保存了一部分
【发布时间】:2018-03-09 19:50:49
【问题描述】:

我在 python 中使用 seaborn 来绘制和保存图形。在 jupyter notebook 中是这样的。

Whole figure

但是当我使用以下代码保存图形时,它显示为这样。 Only part of it

我不知道为什么。这是我的python代码。

whole_pt = whole_rules_df.pivot_table(index='whole_rules_from', columns='whole_rules_to', values='whole_rules_value', aggfunc=np.sum)
f, ax = plt.subplots(figsize=(12,8))
one_heat = sns.heatmap(whole_pt, fmt="d",cmap='YlGnBu', ax=ax,vmin=0,vmax=1)
one_heat.get_figure().savefig('whole_rules.jpg')

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    matplotlib 图形本身就是被裁剪的图形。但是,当使用 jupyter 在内联后端显示 matplotlib 图形时,显示的是图形的已保存 png 版本。这种“保存”是使用bbox_inches="tight" 选项执行的,它将保存的区域放大或裁剪为图形的内容。

    为了在手动保存图形时达到同样的效果,这个选项也必须包括在内,

    fig.savefig("filename.png", bbox_inches="tight")
    

    或者,直接生成内容适合 is 的图形可能很有用。这可以使用fig.subplots_adjust() 方法或调用fig.tight_layout() 来完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-15
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      相关资源
      最近更新 更多