![](/img/trans.png)
[英]How to plot percentage with seaborn distplot / histplot / displot
[英]How to plot uneven number of subplots for seaborn histplot
我目前有一個我正在繪制分布的 13 列的列表。 我想創建一系列子圖,以便這些圖占用更少的空間,但在循環中這樣做很困難。
樣品 DataFrame:
import pandas as pd
import numpy as np
data = {'identifier': ['A', 'B', 'C', 'D'],
'treatment': ['untreated', 'treated', 'untreated', 'treated'], 'treatment_timing': ['pre', 'pre', 'post', 'post'],
'subject_A': [1.3, 0.0, 0.5, 1.6], 'subject_B': [2.0, 1.4, 0.0, 0.0], 'subject_C': [nan, 3.0, 2.0, 0.5],
'subject_D': [np.nan, np.nan, 1.0, 1.6], 'subject_E': [0, 0, 0, 0], 'subject_F': [1.0, 1.0, 0.4, 0.5]}
df = pd.DataFrame(data)
identifier treatment treatment_timing subject_A subject_B subject_C subject_D subject_E subject_F
0 A untreated pre 1.3 2.0 NaN NaN 0 1.0
1 B treated pre 0.0 1.4 3.0 NaN 0 1.0
2 C untreated post 0.5 0.0 2.0 1.0 0 0.4
3 D treated post 1.6 0.0 0.5 1.6 0 0.5
這是我目前擁有的:
fig, axes = plt.subplots(3,5, sharex=True, figsize=(12,6))
for index, col in enumerate(COL_LIST):
sns.histplot(
df ,x=col, hue="time", multiple="dodge", bins=10, ax=axes[index,index % 3]
).set_title(col.replace("_", " "))
plt.tight_layout()
這絕對行不通。 但我不確定是否有一種簡單的方法來定義軸,而不必復制和粘貼這條線 13 次並手動定義軸坐標。
使用 displot 有點麻煩,因為 col_wrap 錯誤
ValueError: Number of rows must be a positive integer, not 0
(我相信這是由於 np.nan 的存在)
seaborn.displot
會更容易,它是一個FacetGrid ,而不是seaborn.histplot
。
row
、 col
和col_wrap
來獲取所需的行數和列數。subject_
列必須堆疊,以將 dataframe 轉換為整齊的格式,可以使用.stack
完成import pandas as pd
import seaborn as sns
# convert the dataframe into a long form with stack
df_long = df.set_index(['identifier', 'treatment', 'treatment_timing']).stack().reset_index().rename(columns={'level_3': 'subject', 0: 'vals'})
# sort by subject
df_long = df_long.sort_values('subject').reset_index(drop=True)
# display(df_long.head())
identifier treatment treatment_timing subject vals
0 A untreated pre subject_A 1.3
1 D treated post subject_A 1.6
2 C untreated post subject_A 0.5
3 B treated pre subject_A 0.0
4 D treated post subject_B 0.0
# plot with displot
sns.displot(data=df_long, row='subject', col='treatment', x='vals', hue='treatment_timing', bins=10)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.