Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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?

1 Answer

0 votes
by (41.4k points)

So, basically axes shape is (nrows, ncols) and in this case:

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)

Now, when we do ax=axes[0], we get an array and not the axes.  

Try the below code:

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

If you wish to learn more about how to use python for data science, then go through this data science python course by Intellipaat for more insights.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...