Seaborn bar plots

Working with Categorical Data in Python

Kasey Jones

Research Data Scientist

Traditional bar chart

# Code provided for clarity
reviews["Traveler type"].value_counts().plot.bar()

A bar chart counting the number of reviews by different traveler types.

Working with Categorical Data in Python

The syntax

sns.set(font_scale=1.3)
sns.set_style("darkgrid")

sns.catplot(x="Traveler type", y="Score", data=reviews, kind="bar")

A seaborn categorical bar plot for hotel reviews given the reviewers traveler type.

Working with Categorical Data in Python

Ordering your categories

reviews["Traveler type"] = reviews["Traveler type"].astype("category")
reviews["Traveler type"].cat.categories
Index(['Business', 'Couples', 'Families', 'Friends', 'Solo'], dtype='object')
Working with Categorical Data in Python

Updated visualization

sns.catplot(x="Traveler type", y="Score", data=reviews, kind="bar")

  • Note: catplot() has an order parameter
Working with Categorical Data in Python

The hue parameter

  • hue:
    • name of a variable in data
    • used to split the data by a second category
    • also used to color the graphic
sns.set(font_scale=1.2)
sns.set_style("darkgrid")
sns.catplot(x="Traveler type", y="Score", data=reviews, kind="bar",
            hue="Tennis court")  # <--- new parameter
Working with Categorical Data in Python

Bar plot across two variables

Working with Categorical Data in Python

Bar plot practice

Working with Categorical Data in Python

Preparing Video For Download...