Complete Seaborn Tutorial

Rina Mondal
Women in Technology
5 min readJun 11, 2024

Seaborn offers a wide variety of plot types to visualize relationships, distributions, and statistical summaries of data.

Here, we’ll explore relational, categorical, and other plots, providing detailed explanations and code examples.

We can draw two categories of plots using Seaborn.

A. Axes-Level Functions: Create individual plots and return a matplotlib.axes object. Mainly used for fine-grained control over single plot details, such as titles, labels, and specific customizations.

B. Figure-Level Functions: Create complex figures with multiple subplots or facets and return a FacetGrid or PairGrid object. Mainly used for creating complex visualizations involving multiple subplots or facets.

  1. Relational Plots: Relational plots help visualize relationships between variables, especially for time series or two continuous variables. It is a figure level function.

i. Scatter Plot: A scatter plot displays the relationship between two continuous variables. It is an Axes level function.

import seaborn as sns
import matplotlib.pyplot as plt
# Load dataset
tips = sns.load_dataset("tips")
# Create scatter plot
sns.scatterplot(x="total_bill", y="tip", data=tips)

ii. Line Plot: A line plot is ideal for visualizing data points over time or sequential data. It is an Axes level function.

# Create line plot
sns.lineplot(x="size", y="total_bill", data=tips)

Relplot: The ‘relplot’ function combines scatter and line plots into one flexible function. It is a figure level function.

# Create relplot
sns.relplot(x="total_bill", y="tip", hue="sex", style="time", size="size", data=tips)
#By default the output is a scatter plot.

#To create a line plot
sns.relplot(x="total_bill", y="tip", hue="sex", style="time", size="size", data=tips, kind='line))

#Though it may not provide any helpful insights, but this is also one way by which you can create different plots

2. Distribution Plots: Seaborn provides powerful tools for visualizing distributions.

i. Histogram: A histogram shows the distribution of a single variable.

# Create histogram
sns.histplot(tips["total_bill"], bins=30, kde=True)

ii. KDE Plot: A KDE plot estimates the probability density function of a continuous variable.

# Create KDE plot
sns.kdeplot(tips["total_bill"], shade=True)

iii. Rug Plot: A rug plot is a one-dimensional representation of a distribution. It displays data points along a single axis, typically at their actual positions.

# Create rug plot
sns.rugplot(tips["total_bill"])

3. Categorical Plots: Categorical plots visualize the distribution of categorical data. . It is a figure level function.

i. Categorical Estimate Plot: A categorical estimate plot, also known as a categorical aggregation plot, is a type of visualization that displays the central tendency (typically the mean or median) of a continuous variable within different categories.

A. Bar Plot: A bar plot represents data with rectangular bars.

# Create bar plot
sns.barplot(x="day", y="total_bill", data=tips)

B. Count Plot: A count plot shows the count of observations in each categorical bin.

# Create count plot
sns.countplot(x="day", data=tips)

B. Categorical Distribution Plot: Illustrates the distribution of a categorical variable in a dataset.

i. Box Plot: A box plot displays the distribution of a continuous variable using quartiles.

# Create box plot
sns.boxplot(x="day", y="total_bill", data=tips)

ii. Violin Plot: A violin plot combines aspects of a box plot and a KDE plot.

# Create violin plot
sns.violinplot(x="day", y="total_bill", data=tips)

Categorical Scatter Plot: A categorical scatter plot, also known as a categorical point plot or a strip plot, is a type of visualization that displays the relationship between one categorical variable and one continuous variable.

i. Strip Plot: A strip plot is a scatter plot for categorical data.

# Create strip plot
sns.stripplot(x="day", y="total_bill", data=tips)

ii. Swarm Plot: A swarm plot adjusts the points in a scatter plot to avoid overlap.

# Create swarm plot
sns.swarmplot(x="day", y="total_bill", data=tips)

Catplot: The ‘catplot’ function is a combination of all categorical plots, with the ability to facet the data.

# Create catplot
sns.catplot(x="day", y="total_bill", kind="violin", hue="sex", data=tips)

Faceting: known as small multiples or trellis displays, involves breaking a plot into multiple subplots based on the values of one or more categorical variables. Seaborn’s catplot() function is specifically designed to create faceted plots.

Here’s how you can use catplot() for faceting:

import seaborn as sns
# Example data
tips = sns.load_dataset("tips")

# Faceting using catplot
sns.catplot(x="day", y="total_bill", hue="sex", kind="swarm", data=tips, height=4, aspect=2)

# Additional customization
plt.title('Total Bill by Day and Gender')
plt.xlabel('Day')
plt.ylabel('Total Bill')
plt.show()

Catplot() offers various kinds of plots (e.g., strip plot, box plot, violin plot) that can be used for faceting, and you can adjust other parameters like color palette, axes labels, and titles as needed.

4. Matrix Plots: Matrix plots visualize data matrices.

i. Heatmap: A heatmap displays data in a matrix format with colors representing values. It’s often used to visualize the correlation between different variables or to highlight patterns in data. For example, in a correlation heatmap, high positive correlations can be represented by one color, while high negative correlations can be represented by another color.

import seaborn as sns

# Load Dataset
flights = sns.load_dataset("flights")

# Create pivot table
flights_pivot = flights.pivot(index="month", columns="year", values="passengers")

# Create heatmap
sns.heatmap(flights_pivot, annot=True, fmt="d")

ii. Clustermap: A clustermap performs hierarchical clustering to organize data. It groups similar rows and columns together based on their values.

# Create clustermap
sns.clustermap(flights_pivot, annot=True, fmt="d")

5. Regression Plots: Seaborn simplifies the visualization of regression relationships.

i. Regplot: regplot allows you to create scatter plots with regression lines. It’s useful for visualizing the relationship between two variables and fitting a regression model to the data.

# Assuming 'tip' is your DataFrame containing the data
sns.set_context("talk")
sns.regplot(x="size", y="total_bill", data=tips)
plt.show()

Linear Regression Plot: A linear regression plot shows the best-fit line for the data.

# Create linear regression plot
sns.lmplot(x="total_bill", y="tip", data=tips)

ii. Residual Plot: A residual plot shows the residuals of a regression.

# Create residual plot
sns.residplot(x="total_bill", y="tip", data=tips)

6. Multi Plots: A multiplot refers to a composite plot consisting of multiple subplots, each displaying different aspects or subsets of the data.

i. Pair Plots: Pair plots are useful for visualizing pairwise relationships.

# Create pair plot
sns.pairplot(tips, hue="sex")

ii. Joint Plot: A function in the Seaborn library used to visualize the relationship between two variables, along with their individual distributions.

sns.jointplot(x="total_bill", y="tip", data=tips, kind="scatter")

Style and Aesthetics: Seaborn allows customization of the look and feel of plots.

Setting Styles:

sns.set_style("whitegrid")
sns.boxplot(x="day", y="total_bill", data=tips)

Customizing Plots: Seaborn allows for extensive customization of plots.

Adding Titles and Labels:

plt.figure(figsize=(10, 6))
sns.barplot(x="day", y="total_bill", data=tips)
plt.xlabel("Day")
plt.ylabel("Total Bill")
plt.title("Tips")

Changing Color Palettes:

sns.set_palette("pastel")
sns.boxplot(x="day", y="total_bill", data=tips)

Adding Annotations:

plt.figure(figsize=(10, 6))
sns.barplot(x="day", y="total_bill", data=tips)
plt.title("Total Bill by Day")
plt.xlabel("Day of the Week")
plt.ylabel("Total Bill")

# Add annotation
plt.annotate('Highest Average', xy=(3, 30), xytext=(2, 35),
arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()

Saving Plots: You can save your plots to files.

plt.figure(figsize=(10, 6))
sns.barplot(x="day", y="total_bill", data=tips)
plt.title("Total Bill by Day")
plt.xlabel("Day of the Week")
plt.ylabel("Total Bill")
plt.savefig("total_bill_by_day.png")
plt.show()

Seaborn provides a wide range of plots to visualize relationships, distributions, and statistical summaries. By learning its functions and customization options, you can create informative and attractive visualizations for your data analysis projects.

--

--

Rina Mondal
Women in Technology

I have an 8 years of experience and I always enjoyed writing articles. If you appreciate my hard work, please follow me, then only I can continue my passion.