添加 seaborn clustermap 以与其他图一起显示 [英] Adding seaborn clustermap to figure with other plots

查看:35
本文介绍了添加 seaborn clustermap 以与其他图一起显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将以下两个图放在同一个图形上:

I am trying to put the following two plots onto the same figure:

import seaborn as sns; sns.set(color_codes=True)
import matplotlib.pyplot as plt
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
iris = sns.load_dataset("iris")
sns.boxplot(data=iris, orient="h", palette="Set2", ax = ax1)
species = iris.pop("species")
lut = dict(zip(species.unique(), "rbg"))
row_colors = species.map(lut)
sns.clustermap(iris, row_colors=row_colors, ax = ax2)

我知道 clustermap 返回一个数字,所以这不起作用.但是,我仍然需要一种方法来将这些图彼此相邻(水平)呈现.sns.heatmap 返回一个轴,但它不支持聚类或颜色注释.

I understand that clustermap returns a figure, so this doesn't work. However, I still need a way to present these plots next to each other (horizontal). sns.heatmap returns an axes, but it does not support clustering or color annotation .

最好的方法是什么?

推荐答案

确实,clustermap 和其他一些 seaborn 函数一样,创建了自己的图形.您对此无能为力,但只要您希望在最终图形中包含的所有其他内容都可以在轴内创建,例如在本例中的 boxplot,解决方案相对简单.

Indeed, clustermap, as some other seaborn functions, creates its own figure. There is nothing you can do about that but as long as all other content you want to have in the final figure can be created inside axes, like in this case the boxplot, the solution is relatively easy.

您可以简单地使用 clustermap 为您创建的图形.然后的想法是操纵轴的网格规格,以便为其他轴留下一些位置.

You can simply work with the figure that clustermap has created for you. The idea would then be to manipulate the gridspec of the axes such that there is some place left for the other axes.

import seaborn as sns; sns.set(color_codes=True)
import matplotlib.pyplot as plt
import matplotlib.gridspec

iris = sns.load_dataset("iris")
species = iris.pop("species")

lut = dict(zip(species.unique(), "rbg"))
row_colors = species.map(lut)

#First create the clustermap figure
g = sns.clustermap(iris, row_colors=row_colors, figsize=(13,8))
# set the gridspec to only cover half of the figure
g.gs.update(left=0.05, right=0.45)

#create new gridspec for the right part
gs2 = matplotlib.gridspec.GridSpec(1,1, left=0.6)
# create axes within this new gridspec
ax2 = g.fig.add_subplot(gs2[0])
# plot boxplot in the new axes
sns.boxplot(data=iris, orient="h", palette="Set2", ax = ax2)
plt.show()

对于具有多个图形级函数来组合解决方案的情况要复杂得多,例如在这个问题中.

For the case when having multiple figure-level functions to combine the solution is much more complicated, as seen e.g. in this question.

这篇关于添加 seaborn clustermap 以与其他图一起显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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