Count plots and bar plots

Introduction to Data Visualization with Seaborn

Erin Case

Data Scientist

Categorical plots

  • Examples: count plots, bar plots
  • Involve a categorical variable
  • Comparisons between groups

Count plot of masculinity responses

Introduction to Data Visualization with Seaborn

catplot()

  • Used to create categorical plots
  • Same advantages of relplot()
  • Easily create subplots with col= and row=
Introduction to Data Visualization with Seaborn

countplot() vs. catplot()

import matplotlib.pyplot as plt
import seaborn as sns

sns.countplot(x="how_masculine",
              data=masculinity_data)

plt.show()

Count plot of masculinity responses

Introduction to Data Visualization with Seaborn

countplot() vs. catplot()

import matplotlib.pyplot as plt
import seaborn as sns

sns.catplot(x="how_masculine",
            data=masculinity_data,
            kind="count")

plt.show()

Count plot of masculinity responses

Introduction to Data Visualization with Seaborn

Changing the order

import matplotlib.pyplot as plt
import seaborn as sns

category_order = ["No answer", "Not at all", "Not very", "Somewhat", "Very"]
sns.catplot(x="how_masculine", data=masculinity_data, kind="count", order=category_order)
plt.show()

Reordered count plot of masculinity responses

Introduction to Data Visualization with Seaborn

Bar plots

Displays mean of quantitative variable per category

import matplotlib.pyplot as plt
import seaborn as sns

sns.catplot(x="day",
            y="total_bill",
            data=tips,
            kind="bar")

plt.show()

Bar plot of average bill per day

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Introduction to Data Visualization with Seaborn

Confidence intervals

  • Lines show 95% confidence intervals for the mean
  • Shows uncertainty about our estimate
  • Assumes our data is a random sample

Bar plot of average bill per day

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Introduction to Data Visualization with Seaborn

Turning off confidence intervals

import matplotlib.pyplot as plt
import seaborn as sns

sns.catplot(x="day",
            y="total_bill",
            data=tips,
            kind="bar",
            ci=None)

plt.show()

Bar plot with no confidence intervals

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Introduction to Data Visualization with Seaborn

Changing the orientation

import matplotlib.pyplot as plt
import seaborn as sns

sns.catplot(x="total_bill",
            y="day",
            data=tips,
            kind="bar")

plt.show()

Horizontal bar plot

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Introduction to Data Visualization with Seaborn

Let's practice!

Introduction to Data Visualization with Seaborn

Preparing Video For Download...