Seaborn is a Python data visualization library built on top of Matplotlib.
The most common way to import Seaborn into your Python environment is to use the following syntax:
import seaborn as sns
The import seaborn portion of the code tells Python to bring the Seaborn library into your current environment.
The as sns portion of the code then tells Python to give Seaborn the alias of sns. This allows you to use Seaborn functions by simply typing sns.function_name rather than seaborn.function_name.
Once you’ve imported Seaborn, you can then use the functions built in it to quickly visualize data.
Set the Seaborn Theme
Once you’ve imported Seaborn, you can set the default theme for plots by using the following function:
sns.set_theme(style='darkgrid')
This function takes the following potential styles as arguments:
- darkgrid (dark background with white gridlines)
- whitegrid (white background with grey gridlines)
- dark (dark background with no gridlines)
- white (white background with no gridlines)
- ticks (white background with axis ticks and no gridlines)
It’s recommended to set the theme after importing the Seaborn library.
Create Your First Plot
Once you’ve imported Seaborn and set the theme, you’re ready to create your first plot.
Seaborn has several built-in plots you can create, including:
- scatterplot
- lineplot
- histplot
- kdeplot
- ecdfplot
- rugplot
- stripplot
- swarmplot
- boxplot
- violinplot
- pointplot
- barplot
For example, here’s how to create a simple scatterplot using the built-in Seaborn tips dataset:
import seaborn as sns
#set theme
sns.set_theme(style='darkgrid')
#load tips dataset
tips = sns.load_dataset('tips')
#create scatterplot
sns.scatterplot(data=tips, x='total_bill', y='tip')
And here’s how to create a violin plot using the same dataset:
import seaborn as sns
#set theme
sns.set_theme(style='dark')
#load tips dataset
tips = sns.load_dataset('tips')
#create scatterplot
sns.violinplot(data=tips, x='total_bill', color='purple')
For a comprehensive overview of Seaborn plotting functions, refer to this documentation page.
Additional Resources
If you want to learn more about Seaborn, check out the complete online seaborn documentation.
For practical applications of Seaborn, check out the following tutorials:
How to Add a Title to Seaborn Plots
How to Adjust the Figure Size of a Seaborn Plot
How to Change the Position of a Legend in Seaborn