plt.tight_layout()与sns.clustermap

时间:2015-12-09 12:41:20

标签: python matplotlib seaborn

Seaborns群集图不适用于plt.tight_layout()。

import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

np.random.seed(98)
df = pd.DataFrame(np.random.rand(10,10))
cm = sns.clustermap(df)

plt.tight_layout()
plt.show()

给出以下错误ValueError: max() arg is an empty sequence。 我还使用了sns.heatmap()完全相同的数据集,这没有给出任何错误。有工作吗? 注意:我使用的实际标签比本例中的标签要长很多,但代码太长而无法显示。谢谢你的帮助!

2 个答案:

答案 0 :(得分:7)

错误的另一部分是消息:

  

此图包含与tight_layout不兼容的轴,因此其结果可能不正确。

我认为tight_layout不适用于clustermap。但是,从sns.clustermap documentation开始,它说:

  

返回的对象有一个savefig方法,如果要保存图形对象而不剪切树形图,则应使用该方法。

因此,在您的示例中,您可以使用cm.savefig('myfigure.png'),它似乎对tight_layout执行类似的操作。

答案 1 :(得分:0)

当我在plt.tight_layout()plt.plot()之前放置plt.xlabel()时出现此错误我只需将其移至plt.show()之前的最后一个命令并且它有效。