![](/img/trans.png)
[英]Move seaborn clustermap row_colors bar to the other side of the plot?
[英]Plot on top of seaborn clustermap
我使用seaborn.clustermap
生成了一個seaborn.clustermap
。 我想在熱圖的頂部繪制/繪制一條水平線,如圖所示
我只是嘗試使用matplotlib:
plt.plot([x1, x2], [y1, y2], 'k-', lw = 10)
但是沒有顯示該行。 seaborn.clustermap
返回的對象沒有像這個類似問題中的任何屬性。 我該如何繪制線條?
以下是生成類似於我發布的“隨機”群集圖的代碼:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.