Creating a Bar Chart in Seaborn, Plotly, and Matplotlib | Data Science Tutorial for Beginners

Stetson Done, MSDA
Data Digest
Published in
5 min readOct 31, 2022
Image Compiled by Author

Today I’ll be walking you through a mini-tutorial to create a bar chart in three different Python visualization libraries. To follow along, download the datasets here.

You can also download the example Jupyter Notebook file here.

When and Why You Should Use a Bar Chart

Bar charts are best for comparing categorical data. They show the total count or frequency of a variable across subgroups in data which allows for easy visual comparison.

About Matplotlib, Seaborn, and Plotly

The libraries we will be exploring today are three popular choices for exploratory data analysis and data visualization.

  1. Matplotlib

Pros: Easy to learn & many other visualization libraries are built on top of the matplotlib framework. Many options for customization.

Cons: Arguably the least visually appealing visualization library. Your plots will work but they likely won’t look great without extensive customization.

Refer to the Matplotlib documentation here.

2. Seaborn

Pros: Great for statistical graphs and easy to use. I regularly use a mix of seaborn for all my exploratory data analysis.

Cons: No options to create interactive visualizations.

Refer to the Seaborn documentation here.

3. Plotly

Pros: Interactive visualizations that are ready for end-user view with little to no customization needed.

Cons: Harder to learn (but worth it). You will need to learn a lot of additional syntax since the plotly framework is not built on top of Matplotlib.

Refer to the Plotly documentation here.

Now that you’ve learned the background, let’s begin!

Creating a Bar Chart — Step By Step Guide

Note: Before beginning the exercise, you will need to open a jupyter notebook file within an environment with python, pandas, matplotlib, seaborn, and plotly installed. See this article for more information on using virtual environments in python & Jupyter Notebookhttps://janakiev.com/blog/jupyter-virtual-envs/

Once you have the environment created and a new Jupyter Notebook file opened, you’re ready to follow along.

#1 — Import Packages

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px

#2 — Load the Data into a DataFrame

We will with the “genre_totals.csv” file available HERE.

This file contains total worldwide video game sales from 1980–2020. The sales are grouped by genre.

# using pd.read_csv to create a dataframedata = pd.read_csv('genre_totals.csv')

When printed, the DataFrame looks like this:

#3 — Bar Chart in Matplotlib

In matplotlib, the base syntax for a bar chart is the following:

plt.bar(x,y)

In the cell below, we use the basic syntax plus a few other lines of code to create and customize the bar chart.

Read the code comments below to see what each piece of code in the is doing.

# resizes the figure
plt.figure(figsize=(12,8))
# code that creates the plotplt.bar(data['genre'], data['sales'])# rotates the genre names on the x axis
plt.xticks(rotation=90)
# creates a title at the top of the chart
plt.title('Total Video Game Sales by Genre (1980-2020)', fontsize=20);

#4 — Bar Chart in Seaborn

In Seaborn, the base syntax for a bar chart / bar plot is the following:

sns.barplot(data = __ , x = __ , y = __ )

In the cell below, we use the basic syntax plus a few other lines of code to create and customize the bar chart.

Read the code comments below to see what each piece of code in the is doing.

# resizes the figure
plt.figure(figsize=(12,8))
# code that creates the bar chart
sns.barplot(data=data, x='genre', y='sales')
# rotates the genres on the x-axis
plt.xticks(rotation=90)
# creates a new title for the chart
plt.title('Total Video Game Sales by Genre (1980-2020)', fontsize=20)
# creates a new label for the x-axis
plt.xlabel('Genre', fontsize=14)
# creates a new label for the y-axis
plt.ylabel('Total Sales', fontsize=14);

#5 — Bar Chart in Plotly Express

In plotly express, the base syntax for a bar chart is the following:

px.bar(data, x = __ , y = __ )

In the cell below, we use the basic syntax plus a few other lines of code to create and customize the bar chart.

Read the code comments below to see what each piece of code in the is doing.

# code that creates the chart, labels are included in the px.bar() parametersfig = px.bar(data, x='genre', y='sales', title='Total Video Game    
Sales by Genre (1980-2020)',
labels={'genre':'Genre', 'sales':'Total Sales'})
fig.show()

You might have noticed the plotly syntax differs greatly from matplotlib and seaborn. In plotly, we can declare the title and axis labels inside of the code that creates the chart. Plotly charts are highly customizable. I recommend looking through their documentation to see dozens of other customization options you can implement for your visualizations.

Congratulations! You’ve now made a bar chart using three different data visualization libraries in python.

If you found this article helpful in any way, please give it a “clap” and follow me for more data science content.

What else do you want to see? What would you add to the bar charts above? Comment here or connect with me on LinkedIn. I’m always happy to talk.

--

--

Stetson Done, MSDA
Data Digest

Master's in Data Analytics. Data Analyst II, Purchasing & Risk Analytics. I am passionate about helping others turn data into actionable insight.