gpt4 book ai didi

python - 在 seaborn clustermap 上绘制

转载 作者:太空狗 更新时间:2023-10-30 00:35:32 27 4
gpt4 key购买 nike

我使用 seaborn.clustermap 生成了一个 clustermap。我想在热图顶部绘制/绘制一条水平线,如图所示 enter image description here

我只是尝试将 matplotlib 用作:

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

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

这是生成“随机”集群图的代码,类似于我发布的那个:

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

enter image description here

关于python - 在 seaborn clustermap 上绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32868423/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com