Seaborn Tutorial for Beginners#

Introduction#

Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics.

Installation#

You can install Seaborn using pip:

pip install seaborn

Importing Seaborn#

To use Seaborn in your code, you need to import it. It’s also common to import matplotlib for additional customization and pandas for data manipulation:

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
print(sns.__version__)
0.12.2

Loading Data#

Seaborn comes with built-in datasets for testing and demonstration. You can load a dataset using sns.load_dataset():

df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv')

Creating a Scatter Plot#

Here’s how you can create a scatter plot with Seaborn:

sns.scatterplot(x='total_bill', y='tip', data=df)
plt.show()
../_images/7465e7598f3e1ef6ad4f2625e2505ff463bc4009601498366e1808882b9656b2.png

Creating a Line Plot#

Here’s how you can create a line plot with Seaborn:

sns.lineplot(x='size', y='tip', data=df)
plt.show()
/Users/zhuyunye/miniconda3/envs/pybook/lib/python3.10/site-packages/seaborn/_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
/Users/zhuyunye/miniconda3/envs/pybook/lib/python3.10/site-packages/seaborn/_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
../_images/ab7aef59783b9f047e7764675ca2885e999df07937a1b8822b9a3bcb70504b7d.png

Creating a Histogram#

Here’s how you can create a histogram with Seaborn:

sns.histplot(df['total_bill'])
plt.show()
/Users/zhuyunye/miniconda3/envs/pybook/lib/python3.10/site-packages/seaborn/_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
../_images/66ea3afa24094c5ac2018f4f1460380f0013ae9f9a8ba5ca7a57fdbb02b1a0bb.png

Creating a Box Plot#

Here’s how you can create a box plot with Seaborn:

sns.boxplot(x='day', y='total_bill', data=df)
plt.show()
../_images/f97c89565219b4e4aab28bdc15a8b92a225ed2b5b85db31f8f20d3bc4a021f49.png

Creating a Heatmap#

Here’s how you can create a heatmap with Seaborn:

correlation = df[['total_bill','tip','size']].corr()
sns.heatmap(correlation, annot=True)
plt.show()
../_images/0bba81290ea2003af087c0009eefffc8802fe0dfa56ffccb4f7ffe580337e730.png

This is a very basic introduction to Seaborn. Seaborn has many more features and plot types which you can explore in the official documentation.