在Seaborn中为Python创建箱线图FacetGrid

在Seaborn中为Python创建箱线图FacetGrid

本文介绍了在Seaborn中为Python创建箱线图FacetGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在seaborn中创建4x4 FacetGrid,用于4个箱形图,根据虹膜数据集中的虹膜种类将每个箱形图分成3个箱形图。目前,我的代码如下:

I'm trying to create a 4x4 FacetGrid in seaborn for 4 boxplots, each of which is split into 3 boxplots based on the iris species in the iris dataset. Currently, my code looks like this:

sns.set(style="whitegrid")
iris_vis = sns.load_dataset("iris")

fig, axes = plt.subplots(2, 2)

ax = sns.boxplot(x="Species", y="SepalLengthCm", data=iris, orient='v',
    ax=axes[0])
ax = sns.boxplot(x="Species", y="SepalWidthCm", data=iris, orient='v',
    ax=axes[1])
ax = sns.boxplot(x="Species", y="PetalLengthCm", data=iris, orient='v',
    ax=axes[2])
ax = sns.boxplot(x="Species", y="PetalWidthCm", data=iris, orient='v',
    ax=axes[3])

但是,我从解释器中收到此错误:

However, I'm getting this error from my interpreter:

AttributeError: 'numpy.ndarray' object has no attribute 'boxplot'

我对属性错误的确切位置感到困惑。我需要更改什么?

I'm confused on where the attribute error is exactly in here. What do I need to change?

推荐答案

形状为(nrows,ncols)。在这种情况下是:

axes shape is (nrows, ncols). In this case is:

array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7f4267f425f8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f4267f1bb38>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x7f4267ec95c0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f4267ef9080>]],
      dtype=object)

因此,当您执行 ax = axes [0] 会得到一个数组,而不是轴。尝试:

So, when you do ax=axes[0] you get a array and not the axes. Try:

fig, axes = plt.subplots(2, 2)

ax = sns.boxplot(x="Species", y="SepalLengthCm", data=iris, orient='v',
    ax=axes[0, 0])
ax = sns.boxplot(x="Species", y="SepalWidthCm", data=iris, orient='v',
    ax=axes[0, 1])
ax = sns.boxplot(x="Species", y="PetalLengthCm", data=iris, orient='v',
    ax=axes[1, 0])
ax = sns.boxplot(x="Species", y="PetalWidthCm", data=iris, orient='v',
    ax=axes[1, 1])

这篇关于在Seaborn中为Python创建箱线图FacetGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:11