如何在Seaborn中以热图的轴表示类 [英] How to express classes on the axis of a heatmap in Seaborn

查看:230
本文介绍了如何在Seaborn中以热图的轴表示类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个非常简单的热图图表,其中Seaborn显示了相似度方矩阵.这是我使用的一行代码:

I created a very simple heatmap chart with Seaborn displaying a similarity square matrix. Here is the one line of code I used:

sns.heatmap(sim_mat, linewidths=0, square=True, robust=True)
sns.plt.show()

这是我得到的输出:

我想做的是在x和y轴上表示实例的标签,而不是实例的标签(彩色的指示器(在每个轴上想象一个类似小图的东西)),其中每种颜色表示与每个实例相关联的另一个变量(比方说,我已将此信息存储在名为labels的列表中),以及在指定热图颜色的那个旁边的另一个图例(关于lmplot的类似信息).两种信息必须具有不同的调色板,这一点很重要.

What I'd like to do is to represent on the x and y axis not the labels of my instances but a colored indicator (imagine something like a small palplot on each axis) where each color represents another variable associated to each instance (let's say I have this info stored a list named labels) plus another legend for this kind of information next to the one specifying the colors of the heatmap (one like that for the lmplot). It is important that the two informations have different color palettes.

在Seaborn中有可能吗?

Is this possible in Seaborn?

更新

我正在寻找的是正确建议的clustermap.

What I am looking for is a clustermap as correctly suggested.

sns.clustermap(sim_mat, row_colors=label_cols, col_colors=label_cols
    row_cluster=False, col_cluster=False)

这是我得到的提示,点和线太小,在文档中看不到放大它们的方法.我想

Here is what I am getting btw, the dots and lines are too small and I do not see a way to enlarge them in the documentation. I'd like to

另外,我该如何添加图例,然后将两个图例放置在另一个位置相同的位置?

Plus, how can I add a legend and put the two one next to the other in the same position?

推荐答案

有两个选项:

首先,heatmap是轴级别的图形,因此您可以为相关矩阵设置一个主要的大主热图轴,并在其侧面添加热图,然后将类颜色传递给自己.这将需要一些工作,但是可以使您对一切的运行方式有很多控制.

First, heatmap is an Axes level figure, so you could set up a main large main heatmap axes for the correlation matrix and flank it with heatmaps that you then pass class colors to yourself. This will be a little bit of work, but gives you lots of control over how everything works.

尽管这在clustermap中或多或少是一个选项,所以我将在这里演示如何以这种方式进行操作.有点hack,但是可以用.

This is more or less an option in clustermap though, so I'm going to demonstrate how to do it that way here. It's a bit of a hack, but it will work.

首先,我们将加载示例数据并进行一些回旋转换以获取类标签的颜色.

First, we'll load the sample data and do a bit of roundabout transformations to get colors for the class labels.

networks = sns.load_dataset("brain_networks", index_col=0, header=[0, 1, 2])
network_labels = networks.columns.get_level_values("network")
network_pal = sns.cubehelix_palette(network_labels.unique().size,
                                    light=.9, dark=.1, reverse=True,
                                    start=1, rot=-2)
network_lut = dict(zip(map(str, network_labels.unique()), network_pal))

network_colors = pd.Series(network_labels).map(network_lut)

接下来,我们调用clustermap进行主图绘制.

Next we call clustermap to make the main plot.

g = sns.clustermap(networks.corr(),

                  # Turn off the clustering
                  row_cluster=False, col_cluster=False,

                  # Add colored class labels
                  row_colors=network_colors, col_colors=network_colors,

                  # Make the plot look better when many rows/cols
                  linewidths=0, xticklabels=False, yticklabels=False)

侧面颜色是用热图绘制的,matplotlib将其视为定量数据,因此,没有直接从中获取图例的简单方法.取而代之的是,我们将添加带有正确颜色和标签的不可见barplot,然后为 that 添加图例.

The side colors are drawn with a heatmap, which matplotlib thinks of as quantitative data and thus there's not a straightforward way to get a legend directly from it. Instead of that, we'll add an invisible barplot with the right colors and labels, then add a legend for that.

for label in network_labels.unique():
    g.ax_col_dendrogram.bar(0, 0, color=network_lut[label],
                            label=label, linewidth=0)
g.ax_col_dendrogram.legend(loc="center", ncol=6)

最后,让我们移动颜色栏以占据行树状图通常所在的空白区域并保存图形.

Finally, let's move the colorbar to take up the empty space where the row dendrogram would normally be and save the figure.

g.cax.set_position([.15, .2, .03, .45])
g.savefig("clustermap.png")

这篇关于如何在Seaborn中以热图的轴表示类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆