将不同的 seaborn 分面网格组合成单个图 [英] combine different seaborn facet grids into single plot

查看:42
本文介绍了将不同的 seaborn 分面网格组合成单个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个不同的数据集,每个数据集都生成了一个分面图

I have three different data sets where I produce a facetplot, each

a = sns.FacetGrid(data1, col="overlap",  hue="comp")
a = (g.map(sns.kdeplot, "val",bw=0.8))

b = sns.FacetGrid(data2, col="overlap",  hue="comp")
b = (g.map(sns.kdeplot, "val",bw=0.8))

c = sns.FacetGrid(data3, col="overlap",  hue="comp")
c = (g.map(sns.kdeplot, "val",bw=0.8))

这些地块中的每一个在一行中都有三个子地块,所以我总共有九个地块.

Each of those plots has three subplots in one row, so in total I have nine plots.

我想在这样的子图设置中组合这些图

I would like to combine these plots, in a subplots setting like this

f, (ax1, ax2, ax3) = plt.subplots(3,1)
ax1.a
ax2.b
ax3.c

我该怎么做?

推荐答案

FacetGrid 创建自己的图形.将几个数字组合成一个不是一件容易的事.此外,不存在可添加到图形的子图行之类的东西.因此,需要单独操纵轴.

A FacetGrid creates its own figure. Combining several figures into one is not an easy task. Additionally, there is no such thing as subplot rows which can be added to a figure. So one would need to manipulate the axes individually.

也就是说,找到解决方法可能会更容易.例如.如果要显示的数据框的结构与问题代码中的结构相同,则可以将数据框组合成具有新列的单个框架,并将其用作构面网格的 row 属性.

That said, it might be easier to find workarounds. E.g. if the dataframes to show have the same structure as it seems to be from the question code, one can combine the dataframes into a single frame with a new column and use this as the row attribute of the facet grid.

import numpy as np; np.random.seed(3)
import pandas as pd
import seaborn.apionly as sns
import matplotlib.pyplot as plt

def get_data(n=266, s=[5,13]):
    val = np.c_[np.random.poisson(lam=s[0], size=n),
                np.random.poisson(lam=s[1], size=n)].T.flatten()
    comp = [s[0]]*n +  [s[1]]*n
    ov = np.random.choice(list("ABC"), size=2*n)
    return pd.DataFrame({"val":val, "overlap":ov, "comp":comp})

data1 = get_data(s=[9,11])
data2 = get_data(s=[7,19])
data3 = get_data(s=[1,27])

#option1 combine
for i, df in enumerate([data1,data2,data3]):
    df["data"] = ["data{}".format(i+1)] * len(df)

data = data1.append(data2)
data = data.append(data3)

bw = 2
a = sns.FacetGrid(data, col="overlap",  hue="comp", row="data")
a = (a.map(sns.kdeplot, "val",bw=bw ))
plt.show()

您还可以遍历数据框和轴以获得所需的结果.

You can also loop over the dataframes and axes to obtain the desired result.

import numpy as np; np.random.seed(3)
import pandas as pd
import seaborn.apionly as sns
import matplotlib.pyplot as plt

def get_data(n=266, s=[5,13]):
    val = np.c_[np.random.poisson(lam=s[0], size=n),
                np.random.poisson(lam=s[1], size=n)].T.flatten()
    comp = [s[0]]*n +  [s[1]]*n
    ov = np.random.choice(list("ABC"), size=2*n)
    return pd.DataFrame({"val":val, "overlap":ov, "comp":comp})

data1 = get_data(s=[9,11])
data2 = get_data(s=[7,19])
data3 = get_data(s=[1,27])

#option2 plot each subplot individually
data = [data1,data2,data3]
bw = 2
fig, axes = plt.subplots(3,3, sharex=True, sharey=True)
for i in range(3):
    for j in range(3):
        x = data[i]
        x = x[x["overlap"] == x["overlap"].unique()[j]]
        for hue in x["comp"].unique():
            d = x[x["comp"] == hue]
            sns.kdeplot(d["val"], ax=axes[i,j], bw=bw, label=hue )

plt.show()

这篇关于将不同的 seaborn 分面网格组合成单个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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