gpt4 book ai didi

python - 如何在seaborn clustermap中将Y刻度标签标记为组/类别?

转载 作者:行者123 更新时间:2023-12-02 07:13:52 28 4
gpt4 key购买 nike

我想制作来自患者的基因存在/不存在数据的聚类图/热图,其中基因将被分组为类别(例如趋化性、内毒素等)并适当标记。我在seaborn文档中没有找到任何这样的选项。我知道如何生成热图,只是不知道如何将 yticks 标记为类别。这是我想要实现的目标的示例(与我的工作无关):

heatmap

此处,yticklabels 一月、二月和三月被赋予组标签 Winter,其他 yticklabel 也具有类似的标签。

最佳答案

我复制了您在seaborn中给出的示例,改编自@Stein的答案here .

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from itertools import groupby
import datetime
import seaborn as sns

def test_table():
months = [datetime.date(2008, i+1, 1).strftime('%B') for i in range(12)]
seasons = ['Winter',]*3 + ['Spring',]*2 + ['Summer']*3 + ['Pre-Winter',]*4
tuples = list(zip(months, seasons))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
d = {i: [np.random.randint(0,50) for _ in range(12)] for i in range(1950, 1960)}
df = pd.DataFrame(d, index=index)
return df

def add_line(ax, xpos, ypos):
line = plt.Line2D([ypos, ypos+ .2], [xpos, xpos], color='black', transform=ax.transAxes)
line.set_clip_on(False)
ax.add_line(line)

def label_len(my_index,level):
labels = my_index.get_level_values(level)
return [(k, sum(1 for i in g)) for k,g in groupby(labels)]

def label_group_bar_table(ax, df):
xpos = -.2
scale = 1./df.index.size
for level in range(df.index.nlevels):
pos = df.index.size
for label, rpos in label_len(df.index,level):
add_line(ax, pos*scale, xpos)
pos -= rpos
lypos = (pos + .5 * rpos)*scale
ax.text(xpos+.1, lypos, label, ha='center', transform=ax.transAxes)
add_line(ax, pos*scale , xpos)
xpos -= .2

df = test_table()

fig = plt.figure(figsize = (10, 10))
ax = fig.add_subplot(111)
sns.heatmap(df)

#Below 3 lines remove default labels
labels = ['' for item in ax.get_yticklabels()]
ax.set_yticklabels(labels)
ax.set_ylabel('')

label_group_bar_table(ax, df)
fig.subplots_adjust(bottom=.1*df.index.nlevels)
plt.show()

给予:

希望有帮助。

关于python - 如何在seaborn clustermap中将Y刻度标签标记为组/类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58854335/

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