简体   繁体   English

具有特定轴标签颜色的热图

[英]Heatmap with specific axis labels coloured

I am trying to plot a heatmap with 2 columns of data from a pandas dataframe. 我正在尝试从熊猫数据框中绘制带有2列数据的热图。 However, I would like to use a 3rd column to label the x axis, ideally by colour though another method such as an additional axis would be equally suitable. 但是,我想使用第三列来标记x轴,理想情况下是用颜色标记,尽管另一种方法(例如附加轴)也同样适用。 My dataframe is: 我的数据框是:

    MUT   SAMPLE   VAR             GROUP
    True  s1       1_1334442_T     CC002
    True  s2       1_1334442_T     CC006
    True  s1       1_1480354_GAC   CC002
    True  s2       1_1480355_C     CC006
    True  s2       1_1653038_C     CC006
    True  s3       1_1730932_G     CC002

... ...

Just to give a better idea of the data; 只是为了更好地了解数据; there are 9 different types of 'GROUP', ~60,000 types of 'VAR' and 540 'SAMPLE's. 有9种不同的“组”,约60,000种“ VAR”和540种“样本”。 I am not sure if this is the best way to build a heatmap in python but here is what I figured out so far: 我不确定这是否是在python中构建热图的最佳方法,但这是到目前为止我所发现的:

pivot = pd.crosstab(df_all['VAR'],df_all['SAMPLE'])
sns.set(font_scale=0.4)
g = sns.clustermap(pivot, row_cluster=False, yticklabels=False, linewidths=0.1, cmap="YlGnBu", cbar=False)
plt.show()

I am not sure how to get 'GROUP' to display along the x-axis, either as an additional axis or just colouring the axis labels? 我不确定如何使“组”沿x轴显示,作为附加轴还是仅对轴标签着色? Any help would be much appreciated. 任何帮助将非常感激。

I'm not sure if the 'MUT' column being a boolean variable is an issue here, df_all is 'TRUE' on every 'VAR' but as pivot is made, any samples which do not have a particular 'VAR' are filled as 0, others are filled with 1. My aim was to try and cluster samples with similar 'VAR' profiles. 我不确定这里是否是布尔变量的'MUT'列是否存在问题,每个'VAR'上的df_all是否为'TRUE',但是当进行数据透视时,所有没有特定'VAR'的样本都会被填充为0,其他填充为1。我的目的是尝试对具有相似“ VAR”配置文件的样本进行聚类。 I hope this helps. 我希望这有帮助。

Please let me know if I can clarify anything further? 请让我知道是否可以进一步澄清? Many thanks 非常感谢

Take look at this example. 看一下这个例子。 You can give a list or a dataframe column to the clustermap function. 您可以将列表或数据clustermap列提供给clustermap函数。 By specifying either the col_colors argument or the row_colors argument you can give colours to either the rows or the columns based on that list. 通过指定col_colors参数或row_colors参数,您可以为基于该列表的行或列提供颜色。

In the example below I use the iris dataset and make a pandas series object that specifies which colour the specific row should have. 在下面的示例中,我使用虹膜数据集并创建一个pandas系列对象,该对象指定特定行应具有的颜色。 That pandas series is given as an argument for row_colors . 该熊猫系列作为row_colors的参数row_colors

iris = sns.load_dataset("iris")
species = iris.pop("species")
lut = dict(zip(species.unique(), "rbg"))
row_colors = species.map(lut)
g = sns.clustermap(iris, row_colors=row_colors,row_cluster=False)

This code results in the following image. 此代码产生以下图像。

You may need to tweak a bit further to also include a legend for the colouring for groups. 您可能需要进一步调整,以包括用于组着色的图例。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM