0

I try to plot some graphs using seaborn.boxplot(). Here's my code:

import seaborn as sbn 
import pandas as pd
import matplotlib.pyplot as plt

fig, axes = plt.subplots(figsize = (15,8), nrows=2, ncols=3);
sbn.boxplot(y=df['national_rank'], orient='v', ax=axes[0], palette='Oranges')
sbn.boxplot(y=df['world_rank'], orient='v', ax=axes[1])
sbn.boxplot(y=df['quality_of_education'], orient='v', ax=axes[2])

sbn.boxplot(y=df['alumni_employment'], orient='v', ax=axes[3], palette='Oranges')
sbn.boxplot(y=df['quality_of_faculty'], orient='v', ax=axes[4], palette='Oranges')
sbn.boxplot(y=df['publications'], orient='v', ax=axes[5], palette='Oranges')

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-105-3c63c384eb3b> in <module>()
      1 fig, axes = plt.subplots(figsize = (15,8), nrows=2, ncols=3);
----> 2 sbn.boxplot(y=df['national_rank'], orient='v', ax=axes[0], palette='Oranges')
      3 sbn.boxplot(y=df['world_rank'], orient='v', ax=axes[1])
      4 sbn.boxplot(y=df['quality_of_education'], orient='v', ax=axes[2])
      5 

3 frames
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py in draw_boxplot(self, ax, kws)
    439                     continue
    440 
--> 441                 artist_dict = ax.boxplot(box_data,
    442                                          vert=vert,
    443                                          patch_artist=True,

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

What can I do wrong? I can't understand what it can be. Thanks a lot for your help.

  • In this case, `axes` is a 2D array, and `axes[0` a 1D array of axes. You can convert `axes` to a 1D array: `fig, axes = plt.subplots(...nrows=2, ncols=3)` and then `axes = np.ravel(axes)`. That way, `axes[0]` will be a single `ax` object. – JohanC May 29 '22 at 18:38
  • 1
    The standard alias for seaborn is `sns`, not `sbn` – Trenton McKinney May 29 '22 at 18:48
  • 1
    As @JohanC stated. Corresponds to this [answer](https://stackoverflow.com/a/69228859/7758804) in the duplicate. – Trenton McKinney May 29 '22 at 18:52

0 Answers0