在 seaborn clustermap 上绘制

Plot on top of seaborn clustermap

我使用 seaborn.clustermap 生成了一个聚类图。 我想 draw/plot 在热图顶部画一条水平线,如图所示

我只是尝试将 matplotlib 用作:

plt.plot([x1, x2], [y1, y2], 'k-', lw = 10)

但是不显示该行。 seaborn.clustermap 返回的对象没有类似 中的任何属性。 我怎样才能画出这条线?

这是生成 "random" 集群图的代码,类似于我发布的那个:

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

data = np.random.random((50, 50))
df = pd.DataFrame(data)
row_colors = ["b" if random.random() > 0.2 else "r"  for i in range (0,50)]
cmap = sns.diverging_palette(133, 10, n=7, as_cmap=True)
result = sns.clustermap(df, row_colors=row_colors, col_cluster = False, cmap=cmap, linewidths = 0)
plt.plot([5, 30], [5, 5], 'k-', lw = 10)
plt.show()

您想要的轴对象隐藏在 ClusterGrid.ax_heatmap 中。此代码找到此轴并简单地使用 ax.plot() 来绘制线。您也可以使用 ax.axhline()。

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

data = np.random.random((50, 50))
df = pd.DataFrame(data)
row_colors = ["b" if random.random() > 0.2 else "r"  for i in range (0,50)]
cmap = sns.diverging_palette(133, 10, n=7, as_cmap=True)
result = sns.clustermap(df, row_colors=row_colors, col_cluster = False, cmap=cmap, linewidths = 0)
print dir(result)  # here is where you see that the ClusterGrid has several axes objects hiding in it
ax = result.ax_heatmap  # this is the important part
ax.plot([5, 30], [5, 5], 'k-', lw = 10)
plt.show()