seaborn没有保存整个数字,只是其中的一部分

时间:2018-03-09 19:50:49

标签: python matplotlib seaborn

我在python中使用seaborn来绘制并保存一个数字。在jupyter笔记本中它看起来像这样。

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')

1 个答案:

答案 0 :(得分:2)

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

要在手动保存图形时实现相同目的,还必须包含此选项,

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

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