Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用seaborn?在一个绘图窗口中对所有分类变量进行多个箱线图?_Python_Seaborn_Boxplot - Fatal编程技术网

Python 使用seaborn?在一个绘图窗口中对所有分类变量进行多个箱线图?

Python 使用seaborn?在一个绘图窗口中对所有分类变量进行多个箱线图?,python,seaborn,boxplot,Python,Seaborn,Boxplot,在下面的代码中,我想循环遍历“variables”中的所有分类变量,并在单个绘图窗口中为所有变量显示单独的“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 variab

在下面的代码中,我想循环遍历“variables”中的所有分类变量,并在单个绘图窗口中为所有变量显示单独的“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")
更新:下面的循环代码似乎有效,但如果可能的话,我希望得到一些清理绘图的帮助(附在下面),即删除空的子绘图窗口和内部轴标记/标签。再次感谢

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")
以下是一种方法:

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")

谢谢但是这段代码创建了一个接一个的内联箱线图窗口。我想看看是否可以在一个窗口内使用类似于
fig,axs=plt.subplot(nrows=2,ncols=3)
的方法在循环外进行子窗口绘制,并通过将
ax=axs[]
传递到
sns.catplot(),调用循环内的每个子窗口。但是这不起作用。是的,谢谢!在看到您的编辑之前,我添加了一个更新,因此添加了另一个指向您答案的更新。谢谢。但是这段代码创建了一个接一个的内联箱线图窗口。我想看看是否可以在一个窗口内使用类似于
fig,axs=plt.subplot(nrows=2,ncols=3)
的方法在循环外进行子窗口绘制,并通过将
ax=axs[]
传递到
sns.catplot(),调用循环内的每个子窗口。但是这不起作用。是的,谢谢!在看到您的编辑之前,我添加了一个更新,因此添加了另一个指向您答案的更新。