plt.tight_layout() 与 sns.clustermap

plt.tight_layout() with sns.clustermap

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(),并且没有出现任何错误。有解决办法吗? 注意:我使用的实际标签比本例中的标签长很多,但代码太长无法显示。感谢您的帮助!

错误的另一部分是消息:

This figure includes Axes that are not compatible with tight_layout, so its results might be incorrect.

我认为 tight_layout 不适用于 clustermap。然而,从sns.clustermap documentation,它说:

The returned object has a savefig method that should be used if you want to save the figure object without clipping the dendrograms.

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

类似的操作

当我将 plt.tight_layout() 放在 plt.plot()plt.xlabel() 之前时出现此错误我只需要将其移至 plt.show() 之前的最后一个命令即可.