Reputation: 31
I have a dataframe of personal characteristics like school grades, age, weight, and height.
I want to investigate the density distribution of these data within a 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)
The dataframe will look like this for a single person but in total 50 persons were interviewed:
+=======+============+============+=======+=======================+
| 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 |
+-------+------------+------------+-------+-----------------------+
..............................................................
+=======+============+============+=======+=======================+
The figure looks like this:
As you can see, there a lot of empty plots and I would like to rearrange the plot that only grids with data are present. In the columns grids should be shown that have the same type
. An example (created with Paint) can be seen below.
Furthermore the x-axis is scaled equally for all columns. How can I scale the x-axis individually (even maybe logarithmic).
rearranged figure (with Paint)
Thanks in advance for your support, Christian
Upvotes: 3
Views: 3272
Reputation: 211
If you just want to hide plots for presentation purpose (but keep the overall grid structure):
for (i,j,k), data in fg.facet_data():
if data.empty:
ax = fg.facet_axis(i, j)
ax.set_axis_off()
Upvotes: 9