How to use Seaborn in Python

1 min

Why Seaborn?

Seaborn is a fantastic library to do some data visualization and it is built on top of the Matplotlib library.

Seaborn pushed the simplicity of data exploration and data analysis to the extreme.

How to import it

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
How to import Seaborn and other useful libraries

Setting the theme

How to change the theme in Seaborn

Loading a dataset

Seaborn is made for data science, so it comes with a lot of available example datasets. At the time of this writing, the library has around 19 different datasets that you can use to practice data exploration and data visualization.

Here we import the tips dataset. This dataset contains servers' tips on a given day, the sex, etc...

One could try to predict the amount that people tip given those characteristics.

tips = sns.load_dataset("tips")

A basic plot

Let's do a basic plot that plots the total_bill and the tip that the customer left in order to see whether there is a linear relationship or not.

sns.relplot(x="total_bill", y="tip", data=tips)
Here is how to plot a scatter plot that shows the relation between two variables
Here is the result

Adding some colors

We could and it is always nice to add an extra variable as colors.

Here we have a categorical variable called smoker which tells us whether or not the customer was a smoker.

Here is the plot

sns.relplot(x="total_bill", y="tip", hue="smoker", data=tips)
Can we see an impact if the user smoke or not?