使用 seaborn 在一个绘图 window 中绘制所有分类变量的多个箱线图?
Multiple boxplots of all categorical variables in one plotting window using seaborn?
在下面的代码中,我想遍历 "variables" 中的所有分类变量,并在单个绘图 window 中为所有这些变量显示单独的箱线图 "fare"。我怎么做?谢谢
import seaborn as sns
sns.set(style="ticks")
titanic = sns.load_dataset("titanic")
variables = list(titanic.select_dtypes(include="object").columns) # list of categorical variables
# single boxplot of fare vs passenger sex
g = sns.catplot(x="sex", y="fare", kind="box", data=titanic.query("fare>0"))
g.set(yscale="log")
更新:以下循环代码似乎有效,但如果可能的话,我想要一些帮助清理绘图(附在下面),即删除空子图 window 和内轴 ticks/labels.再次感谢。
fig, axs = plt.subplots(nrows=2, ncols=3)
i = j = 0
for variable in variables:
g = sns.boxplot(x=variable, y="fare", data=titanic.query("fare>0"), ax=axs[i][j])
g.set(yscale="log")
j += 1
if j>2:
i += 1; j = 0
更新#2:下面的 YOLO 代码完成了这项工作。谢谢!
方法如下:
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(15,10))
for i, c in enumerate(variables, 1):
plt.subplot(2,3,i)
g = sns.boxplot(x=c, y="fare",data=titanic.query("fare>0"))
g.set(yscale="log")
在下面的代码中,我想遍历 "variables" 中的所有分类变量,并在单个绘图 window 中为所有这些变量显示单独的箱线图 "fare"。我怎么做?谢谢
import seaborn as sns
sns.set(style="ticks")
titanic = sns.load_dataset("titanic")
variables = list(titanic.select_dtypes(include="object").columns) # list of categorical variables
# single boxplot of fare vs passenger sex
g = sns.catplot(x="sex", y="fare", kind="box", data=titanic.query("fare>0"))
g.set(yscale="log")
更新:以下循环代码似乎有效,但如果可能的话,我想要一些帮助清理绘图(附在下面),即删除空子图 window 和内轴 ticks/labels.再次感谢。
fig, axs = plt.subplots(nrows=2, ncols=3)
i = j = 0
for variable in variables:
g = sns.boxplot(x=variable, y="fare", data=titanic.query("fare>0"), ax=axs[i][j])
g.set(yscale="log")
j += 1
if j>2:
i += 1; j = 0
更新#2:下面的 YOLO 代码完成了这项工作。谢谢!
方法如下:
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(15,10))
for i, c in enumerate(variables, 1):
plt.subplot(2,3,i)
g = sns.boxplot(x=c, y="fare",data=titanic.query("fare>0"))
g.set(yscale="log")