Python seaborn中clustermap的标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34572177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:11:21  来源:igfitidea点击:

Labels for clustermap in seaborn?

pythonmatplotlibmachine-learningartificial-intelligenceseaborn

提问by Bin

I have several questions about labeling for clustermapin seaborn. First is it possible to extract the the distance values for the hierarchical clustering, and plot the value on the tree structure visualization (maybe only the first three levels).

我有几个关于标记clustermapin 的问题seaborn。首先是否可以提取层次聚类的距离值,并在树状结构可视化上绘制该值(可能只有前三个级别)。

Here is my example code for creating a clustermap plot:

这是我创建集群图的示例代码:

import pandas as pd
import numpy as np
import seaborn as sns
get_ipython().magic(u'matplotlib inline')

m = np.random.rand(50, 50)
df = pd.DataFrame(m, columns=range(4123, 4173), index=range(4123, 4173))
sns.clustermap(df, metric="correlation")

enter image description here

在此处输入图片说明

The other two questions are: - How to rotate the y labels since they overlaps together.
- How to move the color bar to the bottom or right. (There was a questionfor heatmap, but does not work for my case. Also does not address the color bar position)

另外两个问题是: - 如何旋转 y 标签,因为它们重叠在一起。
- 如何将颜色条移动到底部或右侧。(有一个关于热图的问题,但不适用于我的情况。也没有解决颜色条位置)

采纳答案by Charles Menguy

I had the exact same issue with the labels on the y-axis being rotated and found a solution. The issue is that if you do plt.yticks(rotation=0)like suggested in the question you referenced, it will rotate the labels on your colobar due to the way ClusterGridworks.

我在旋转 y 轴上的标签时遇到了完全相同的问题并找到了解决方案。问题是,如果您确实plt.yticks(rotation=0)喜欢您引用的问题中的建议,它会根据工作方式旋转您的 colobar 上的标签ClusterGrid

To solve it and rotate the right labels, you need to reference the Axesfrom the underlying Heatmapand rotate these:

要解决它并旋转正确的标签,您需要Axes从底层引用Heatmap并旋转这些:

cg = sns.clustermap(df, metric="correlation")
plt.setp(cg.ax_heatmap.yaxis.get_majorticklabels(), rotation=0)

For your other question about the colorbar placement, I don't think this is supported at the moment, as indicated by this Github issueunfortunately.

对于您关于颜色栏位置的其他问题,我认为目前不支持这一点,不幸的是,正如Github 问题所示

And finally for the hierarchical clustering distance values, you can access the linkage matrics for rows or columns with:

最后对于层次聚类距离值,您可以访问行或列的链接矩阵:

cg = sns.clustermap(df, metric="correlation")
cg.dendrogram_col.linkage # linkage matrix for columns
cg.dendrogram_row.linkage # linkage matrix for rows

回答by tikacp

you can move the colorbar around by changing the position of it's axis cax: cg.cax.set_position((.85,.1,.1,.1)), for instance, where (a,b,c,d) are the x starting position, y starting position, x width and y height of the axis, respectively, in terms of axis coordinates.

您可以通过更改其轴cax的位置来移动颜色条cg.cax.set_position((.85,.1,.1,.1))例如,其中 (a,b,c,d) 分别是轴的 x 起始位置、y 起始位置、x 宽度和 y 高度,在轴坐标方面。

回答by Surya

import seaborn as sns   
g = sns.clustermap(heatmap_df, metric="correlation") 

plt.setp(g.ax_heatmap.get_yticklabels(), rotation=0)  # For y axis
plt.setp(g.ax_heatmap.get_xticklabels(), rotation=90) # For x axis

回答by Nikita Kotlov

A bit different way to rotate labels

旋转标签的方式有点不同

g.ax_heatmap.set_yticklabels(g.ax_heatmap.get_yticklabels(), rotation=0)