python - 在 seaborn clustermap 上绘制

标签 python pandas matplotlib plot seaborn

我使用 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/

相关文章:

python - 如何在 3-D 等高线图中绘制回归预测数据?

python - Pandas- 以值是否大于 0 为条件将一列除以另一列?

python - 使用同一 Dataframe 中另一列的 int 作为索引获取列中的列表值

python - 导入错误 : No module named mpl_toolkits

python - 如何从类型别名中确定类型?

python - 使用 pySerial 在 Python 下重新连接到 USB 端口时,如何避免在 ttyUSB0 和 ttyUSB1 之间切换?

python - 在 pandas python 中按列计算匹配部分字符串的出现次数

python - 在 matplotlib 中将 Y 轴的标题旋转为水平

python - 如何使用 matplotlib 将 RGB 颜色值分配给网格网格

python - 在 python 脚本中运行 sqoop