我有一个包含个人特征的数据框,例如学校成绩、年龄、体重和身高。我想调查这些数据在 seaborn 中的密度分布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)
一个人的数据框看起来像这样,但总共采访了 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 |
+-------+------------+------------+-------+-----------------------+
..............................................................
+=======+============+============+=======+=======================+
该图如下所示:
如您所见,有很多空图,我想重新排列仅存在带有数据的网格的图。在列中应显示具有相同的网格type
。下面可以看到一个示例(使用 Paint 创建)。此外,所有列的 x 轴均按比例缩放。如何单独缩放 x 轴(甚至可能是对数)。
提前感谢您的支持,克里斯蒂安