Seaborn

Language: Python

Data Science

Seaborn was created by Michael Waskom in 2014 to simplify the creation of complex statistical plots. It integrates closely with Pandas data structures and makes it easy to generate visualizations that include summaries of datasets and categorical relationships.

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

Installation

pip: pip install seaborn
conda: conda install seaborn

Usage

Seaborn simplifies the process of creating visualizations such as bar plots, box plots, violin plots, heatmaps, and pair plots. It provides aesthetic defaults and works directly with Pandas DataFrames.

Simple histogram

import seaborn as sns
sns.histplot([1,1,2,3,5])

Plots a histogram of the given list of values using Seaborn’s default styling.

Scatter plot with regression line

import seaborn as sns
import pandas as pd
df = pd.DataFrame({'x':[1,2,3,4],'y':[2,3,5,7]})
sns.regplot(x='x', y='y', data=df)

Creates a scatter plot and automatically fits a regression line to the data.

Boxplot for categorical data

import seaborn as sns
import pandas as pd
df = pd.DataFrame({'category':['A','A','B','B'], 'value':[10,12,20,22]})
sns.boxplot(x='category', y='value', data=df)

Visualizes the distribution of values for each category using a boxplot.

Heatmap

import seaborn as sns
import numpy as np
data = np.random.rand(5,5)
sns.heatmap(data, annot=True, cmap='coolwarm')

Creates a heatmap of a 2D dataset with annotations and a custom color map.

Pairplot for relationships

import seaborn as sns
import pandas as pd
df = sns.load_dataset('iris')
sns.pairplot(df, hue='species')

Plots pairwise relationships in a dataset, colored by species.

Error Handling

ValueError: Could not interpret input: Ensure the column names used in the plot match those in the DataFrame.
ImportError: No module named 'seaborn': Install Seaborn using pip or conda before importing.

Best Practices

Use Pandas DataFrames for structured data input.

Leverage Seaborn’s built-in themes for visually appealing plots.

Combine with Matplotlib for custom modifications.

Use hue, style, and size parameters to enhance multi-dimensional plots.

Always label axes and provide legends for clarity.