Mathew K Analytics

Lesson 16 · Data visualisation in python

Visualizing Categorical Data in Python with Seaborn: Bar, Count, and Swarm Plots

In this lesson, we will explore different types of categorical plots: Bar plots Count plots Swarm plots You will learn how to create these charts, when to…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Welcome to Categorical Plots in Python!#

In this lesson, we will explore different types of categorical plots:

  • Bar plots
  • Count plots
  • Swarm plots

You will learn how to create these charts, when to use each one, and how to read them.

Let us get started!

# Import the tools we will use
import warnings; warnings.filterwarnings("ignore")
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

What are categorical variables?#

Categorical variables are values that represent groups or categories. They are not numbers, but labels or names.

Examples: gender, color, brand, or product type.

# Let us see some data to practice plotting
tips = sns.load_dataset("tips")

print("Shape of data:", tips.shape)
tips.head()
Shape of data: (244, 7)
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

Bar Plot: Comparing Categories#

A bar plot shows how one variable changes across different groups.

For example, we can compare the average size of tips between days.

# Create a bar plot: Average tip by day
plt.figure(figsize=(6,4))
sns.barplot(data=tips, x="day", y="tip")
plt.title("Average Tip by Day")
plt.show()
No description has been provided for this image
# You can quickly see differences by adding color
plt.figure(figsize=(6,4))
sns.barplot(data=tips, x="day", y="tip", hue="sex")
plt.title("Average Tip by Day and Gender")
plt.show()
No description has been provided for this image

Count Plot: Counting How Many in Each Category#

A count plot simply counts how many times each category appears.

It is useful for seeing popularity or balance between groups.

# Count the number of bills for each day
plt.figure(figsize=(6,4))
sns.countplot(data=tips, x="day")
plt.title("Number of Bills by Day")
plt.show()
No description has been provided for this image
# Count plot can also use hue for a second category
plt.figure(figsize=(6,4))
sns.countplot(data=tips, x="day", hue="smoker")
plt.title("Bills by Day and Smoking Status")
plt.show()
No description has been provided for this image

Swarm Plot: Seeing the Actual Values#

Swarm plots show all the actual data points. This helps to spot clusters and spread.

Swarm plots are great for showing patterns in smaller datasets, or when you want to see every record.

# Swarm plot: Each dot is a tip value for that day
plt.figure(figsize=(6,4))
sns.swarmplot(data=tips, x="day", y="tip")
plt.title("All Tips by Day - Swarm Plot")
plt.show()
No description has been provided for this image
# We can color the dots using another category
plt.figure(figsize=(6,4))
sns.swarmplot(data=tips, x="day", y="tip", hue="sex")
plt.title("Swarm Plot by Day and Gender")
plt.show()
No description has been provided for this image

When to Use Each Plot?#

  • Bar plot: To compare averages between groups.
  • Count plot: To see frequencies or how many.
  • Swarm plot: To see all actual data points and spot clusters.

Some plots work better for small data; some for big summaries.

# Try your own: Count plot for time of day
plt.figure(figsize=(6,4))
sns.countplot(data=tips, x="time")
plt.title("Lunch vs Dinner Counts")
plt.show()
No description has been provided for this image
# Mini-project: Which gender tips more at lunch vs dinner?
plt.figure(figsize=(7,5))
sns.barplot(data=tips, x="time", y="tip", hue="sex", ci=None)
plt.title("Average Tip Amount by Meal and Gender")
plt.show()
No description has been provided for this image
# Practice: Swarm plot of total bill by gender
plt.figure(figsize=(7,5))
sns.swarmplot(data=tips, x="sex", y="total_bill", palette="Accent")
plt.title("Swarm Plot of Total Bill by Gender")
plt.show()
No description has been provided for this image

Extra Tips#

  • Use plt.tight_layout() if labels look squished.
  • Experiment with 'palette' for different plot colors.
  • Always label your charts so others understand them.

Exploring with your own data makes these skills stick!

# Challenge: Try making a bar plot with another dataset using input()!
csv_url = input("Paste a CSV url (or press Enter to skip): ")
if csv_url:
    your_data = pd.read_csv(csv_url)
    print("Columns:", list(your_data.columns))
    x = input("Column for X axis: ")
    y = input("Column for Y axis (average height): ")
    plt.figure(figsize=(6,4))
    sns.barplot(data=your_data, x=x, y=y)
    plt.title("Your Bar Plot")
    plt.show()
else:
    print("No data loaded. Try again later!")
    
Columns: ['Month', 'Passengers']
No description has been provided for this image

Recap#

  • You learned what categorical variables are.
  • You created bar, count, and swarm plots.
  • You practiced using different categories and visual styles.

Try using these skills with surveys, sales, or game data.

Next Steps and YouTube Call to Action#

Keep practicing by making your own plots for school projects or hobbies. Watch other videos on data and visualization to deepen your skills.

If you enjoyed this lesson, give us a like and subscribe for more!

Happy coding!

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.