如何避免Seaborn FacetGrid中的空网格

Chr*_*anH 3 python seaborn facet-grid

我有一个个人特征的数据框,例如学校成绩,年龄,体重和身高。我想调查这些数据在海洋中的密度分布Facetgrid

import pandas as pd
import seaborn as sns
import random
import matplotlib.pyplot as plt

# creation of artifical data
random.seed = 10
high = [random.uniform(3.0,6.0) for i in range(50)]
uni = [random.uniform(1.0, 4.0) for i in range(50)]
math = [random.uniform(1.0, 6.0) for i in range(50)]
bio = [random.uniform(1.0, 6.0) for i in range(50)]
history = [random.uniform(1.0, 6.0) for i in range(50)]
age = [random.randint(15,45) for i in range(50)]
height = [random.randint(150,210) for i in range(50)]
weight = [random.randint(50,100) for i in range(50)]

df = pd.DataFrame()
df["value"] = high + uni + math + bio + history + age + height + weight
df["type"] = 100*["final_exam"] + 150*["grade"] + 150*["body"]
df["id"] = 50*["highschool"] + 50*["university"] + 50*["math"] + 50*["bio"]    + 50*["history"] + 50*["age"] + 50*["heigt"] + 50*["weight"]
df["group"] = "A"
df = df[["group", "id", "type", "value"]]
df["para"] =df[["type", "id"]].apply(lambda x: "_".join(x), axis=1)


# Plotting function
def plot_poll(df, **kwargs):

    def plot_densitiy_distribution(data, **kwargs):
        sns.kdeplot(data["value"], shade=True)

    grid_ts = sns.FacetGrid(df, sharey=False, legend_out=True,    hue="group",col="type", row="id")
    grid_ts = grid_ts.map_dataframe(plot_densitiy_distribution)
    plt.tight_layout()
    plt.show()

# main
plot_poll(df)   
Run Code Online (Sandbox Code Playgroud)

一个人的数据框看起来像这样,但总共采访了50个人:

+=======+============+============+=======+=======================+  
| group |     id     |    type    | value |          para         |
+=======+============+============+=======+=======================+   
|   A   | highschool | final_exam |  2.7  | final_exam_highschool |
+-------+------------+------------+-------+-----------------------+
|   A   | university | final_exam |  2.0  | final_exam_university |
+-------+------------+------------+-------+-----------------------+
|   A   |    math    |    grade   |  3.3  |     grade_math        |
+-------+------------+------------+-------+-----------------------+
   ..............................................................
+-------+------------+------------+-------+-----------------------+
|   A   |    age     |    body    |  27   |        body_age       |
+-------+------------+------------+-------+-----------------------+
   ..............................................................
+=======+============+============+=======+=======================+
Run Code Online (Sandbox Code Playgroud)

该图如下所示:

在此处输入图片说明

如您所见,这里有很多空图,我想重新排列图,只显示带有数据的网格。在列中,应显示具有相同网格的网格type。下面是一个示例(使用Paint创建)。此外,x轴对所有列均等缩放。如何单独缩放x轴(甚至可能是对数)。

重排图形(使用Paint)

预先感谢您的支持,克里斯蒂安

Ale*_*tte 5

如果仅出于显示目的而隐藏图(但保留整体网格结构):

    for (i,j,k), data in fg.facet_data():
        if data.empty:
            ax = fg.facet_axis(i, j)
            ax.set_axis_off()
Run Code Online (Sandbox Code Playgroud)