我使用seaborn.clustermap
生成了一个群集图。
我想在热图的顶部绘制/绘制一条水平线,如图
我只是尝试使用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()
答案 0 :(得分:10)
您想要的轴对象隐藏在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()