20 Seaborn concepts with Before-and-After Examples

Anix LynchAnix Lynch
12 min read

1. Line Plot (seaborn.lineplot) ๐Ÿ“‰

Boilerplate Code:

import seaborn as sns

Use Case: Create a line plot to display trends over time or ordered data. ๐Ÿ“‰

Goal: Visualize continuous data over a range or time. ๐ŸŽฏ

Sample Code:

# Example data
data = [1, 2, 3, 4, 5]
sns.lineplot(x=[1, 2, 3, 4, 5], y=[2, 4, 6, 8, 10])

Before Example: You have a sequence of data points but no clear trend visualization. ๐Ÿค”

Data: x = [1, 2, 3, 4, 5], y = [2, 4, 6, 8, 10]

After Example: With lineplot, the trend of the data is clear! ๐Ÿ“‰

Output: A line plot connecting the points.

Challenge: ๐ŸŒŸ Try adding markers with markers=True to make each data point stand out.


2. Bar Plot (seaborn.barplot) ๐Ÿ“Š

Boilerplate Code:

import seaborn as sns

Use Case: Create a bar plot to compare categorical data. ๐Ÿ“Š

Goal: Compare means or other summary statistics across categories. ๐ŸŽฏ

Sample Code:

# Example data
sns.barplot(x=['A', 'B', 'C'], y=[10, 20, 30])

Before Example: You have categories and values but no visual way to compare them. ๐Ÿค”

Data: x = ['A', 'B', 'C'], y = [10, 20, 30]

After Example: With barplot, you can compare the values across categories! ๐Ÿ“Š

Output: A bar chart showing categorical comparisons.

Challenge: ๐ŸŒŸ Try adding error bars using the ci parameter to represent uncertainty.


3. Count Plot (seaborn.countplot) ๐Ÿ“Š

Boilerplate Code:

import seaborn as sns

Use Case: Create a count plot to show the count of observations in each category. ๐Ÿ“Š

Goal: Visualize the frequency of each category. ๐ŸŽฏ

Sample Code:

# Example data
sns.countplot(x=['cat', 'dog', 'dog', 'cat', 'bird'])

Before Example: You have categorical data but no way to visualize how often each category appears. ๐Ÿค”

Data: ['cat', 'dog', 'dog', 'cat', 'bird']

After Example: With countplot, you see how frequently each category appears! ๐Ÿ“Š

Output: A bar plot showing the count of each category.

Challenge: ๐ŸŒŸ Try using hue to split the counts by an additional categorical variable.


4. Histogram (seaborn.histplot) ๐Ÿ“ˆ

Boilerplate Code:

import seaborn as sns

Use Case: Create a histogram to visualize the distribution of numerical data. ๐Ÿ“ˆ

Goal: Display the frequency of data points within bins. ๐ŸŽฏ

Sample Code:

# Example data
sns.histplot(data=[1, 2, 2, 3, 3, 4, 5], bins=5)

Before Example: You have numerical data but no way to visualize its distribution. ๐Ÿค”

Data: [1, 2, 2, 3, 3, 4, 5]

After Example: With histplot, the data distribution becomes clear! ๐Ÿ“ˆ

Output: A histogram showing data frequencies.

Challenge: ๐ŸŒŸ Try changing the number of bins to see how it affects the histogram.


5. Box Plot (seaborn.boxplot) ๐Ÿ“ฆ

Boilerplate Code:

import seaborn as sns

Use Case: Create a box plot to show the distribution of data and detect outliers. ๐Ÿ“ฆ

Goal: Summarize the data distribution using quartiles and outliers. ๐ŸŽฏ

Sample Code:

# Example data
sns.boxplot(data=[1, 2, 3, 4, 5, 6, 7, 8, 9])

Before Example: You have numerical data but no way to visualize its range, quartiles, and outliers. ๐Ÿค”

Data: [1, 2, 3, 4, 5, 6, 7, 8, 9]

After Example: With boxplot, you can see the distribution and detect any outliers! ๐Ÿ“ฆ

Output: A box plot summarizing the data.

Challenge: ๐ŸŒŸ Try splitting the box plots by categories using x and y arguments.


6. Violin Plot (seaborn.violinplot) ๐ŸŽป

Boilerplate Code:

import seaborn as sns

Use Case: Create a violin plot to show the distribution of data and its probability density. ๐ŸŽป

Goal: Combine the benefits of box plots and density plots. ๐ŸŽฏ

Sample Code:

# Example data
sns.violinplot(data=[1, 2, 3, 4, 5, 6, 7, 8, 9])

Before Example: You have numerical data but want a more detailed look at its distribution than just a box plot. ๐Ÿค”

Data: [1, 2, 3, 4, 5, 6, 7, 8, 9]

After Example: With violinplot, you visualize both the distribution and probability density! ๐ŸŽป

Output: A violin plot showing data density and distribution.

Challenge: ๐ŸŒŸ Try splitting violins by categories to compare distributions between groups.


7. Scatter Plot (seaborn.scatterplot) ๐Ÿ”ต

Boilerplate Code:

import seaborn as sns

Use Case: Create a scatter plot to show the relationship between two numerical variables. ๐Ÿ”ต

Goal: Visualize individual data points to detect patterns or correlations. ๐ŸŽฏ

Sample Code:

# Example data
sns.scatterplot(x=[1, 2, 3, 4], y=[10, 11, 12, 13])

Before Example: You have two variables but no way to visualize their relationship. ๐Ÿค”

Data: x = [1, 2, 3, 4], y = [10, 11, 12, 13]

After Example: With scatterplot, the relationship between the variables is visualized! ๐Ÿ”ต

Output: A scatter plot showing individual data points.

Challenge: ๐ŸŒŸ Try adding a third dimension by mapping a categorical variable to hue.


8. Pair Plot (seaborn.pairplot) ๐Ÿ”€

Boilerplate Code:

import seaborn as sns

Use Case: Create a pair plot to visualize relationships between multiple pairs of variables. ๐Ÿ”€

Goal: Show scatter plots for each pair of variables in a dataset. ๐ŸŽฏ

Sample Code:

# Example data
sns.pairplot(sns.load_dataset("iris"))

Before Example: You have multiple variables but no way to visualize their relationships in one view. ๐Ÿค”

Data: Iris dataset (sepal length, sepal width, petal length, petal width)

After Example: With pairplot, you can view scatter plots for each pair of variables! ๐Ÿ”€

Output: A grid of scatter plots for each pair of variables.

Challenge: ๐ŸŒŸ Try using hue to differentiate categories in the dataset.


9. Heatmap (seaborn.heatmap) ๐Ÿ”ฅ

Boilerplate Code:

import seaborn as sns

Use Case: Create a heatmap to visualize data intensity in a matrix. ๐Ÿ”ฅ

Goal: Represent numerical data as color-coded intensity. ๐ŸŽฏ

Sample Code:

# Example data
import numpy as np
data = np.array([[1, 2], [3, 4]])
sns.heatmap(data)

Before Example: You have a matrix of numerical data but no clear way to visualize the intensity. ๐Ÿค”

Data: [[1, 2], [3, 4]]

After Example: With heatmap, you visualize the data intensity using colors! ๐Ÿ”ฅ

Output: A heatmap representing the matrix.

Challenge: ๐ŸŒŸ Try using annot=True to show the data values inside the heatmap cells.


10. Joint Plot (seaborn.jointplot) ๐ŸŒ

Boilerplate Code:

import seaborn as sns

Use Case: Create a joint plot to visualize both the distribution and relationship of two variables. ๐ŸŒ

Goal: Combine scatter plots and distribution plots into one view. ๐ŸŽฏ

Sample Code:

# Example data
sns.jointplot(x=[1, 2, 3], y=[4, 5, 6])

Before Example: You have two variables but no way to visualize both their relationship and distribution. ๐Ÿค”

Data: x = [1, 2, 3], y = [4, 5, 6]

After Example: With jointplot, you can visualize both the scatter plot and histograms in one! ๐ŸŒ

Output: A combined scatter and histogram plot.

Challenge: ๐ŸŒŸ Try using different kind options like reg for regression or hex for hexbin plots.


11. Regression Plot (seaborn.regplot) ๐Ÿ“ˆ

Boilerplate Code:

import seaborn as sns

Use Case: Create a regression plot to display a linear regression line with data points. ๐Ÿ“ˆ

Goal: Visualize the relationship between two variables with a regression line. ๐ŸŽฏ

Sample Code:

# Example data
sns.regplot(x=[1, 2, 3, 4], y=[5, 6, 7, 8])

Before Example: You have two variables but no way to represent their linear relationship. ๐Ÿค”

Data: x = [1, 2, 3, 4], y = [5, 6, 7, 8]

After Example: With regplot, you visualize the relationship with a linear regression line! ๐Ÿ“ˆ

Output: A scatter plot with a regression line.

Challenge: ๐ŸŒŸ Try adding ci=None to remove the confidence interval or change the order for polynomial regression.


12. KDE Plot (seaborn.kdeplot) ๐Ÿ“Š

Boilerplate Code:

import seaborn as sns

Use Case: Create a Kernel Density Estimate (KDE) plot to visualize the distribution of data. ๐Ÿ“Š

Goal: Show the probability density function of a variable. ๐ŸŽฏ

Sample Code:

# Example data
sns.kdeplot(data=[1, 2, 2, 3, 4, 5])

Before Example: You have numerical data but no smooth representation of its distribution. ๐Ÿค”

Data: [1, 2, 2, 3, 4, 5]

After Example: With kdeplot, you see a smooth curve representing the data's distribution! ๐Ÿ“Š

Output: A KDE plot showing the probability density.

Challenge: ๐ŸŒŸ Try combining kdeplot with histplot for a detailed view of the distribution.


13. Facet Grid (seaborn.FacetGrid) ๐Ÿ“

Boilerplate Code:

import seaborn as sns

Use Case: Create a facet grid to plot multiple plots based on subsets of data. ๐Ÿ“

Goal: Display the relationship between variables across different subsets of data. ๐ŸŽฏ

Sample Code:

# Load example data
df = sns.load_dataset("tips")

# Create FacetGrid
g = sns.FacetGrid(df, col="sex", row="time")
g.map(sns.scatterplot, "total_bill", "tip")

Before Example: You have data split by categories but no way to visualize each subset clearly. ๐Ÿค”

Data: Tips dataset with sex and time categories.

After Example: With FacetGrid, you get multiple plots for each subset of the data! ๐Ÿ“

Output: A grid of scatter plots showing tips by total bill.

Challenge: ๐ŸŒŸ Try using hue to add another categorical dimension to the grid.


14. Swarm Plot (seaborn.swarmplot) ๐Ÿ

Boilerplate Code:

import seaborn as sns

Use Case: Create a swarm plot to display categorical scatter plots with non-overlapping points. ๐Ÿ

Goal: Show the distribution of data points within categories without overlapping. ๐ŸŽฏ

Sample Code:

# Example data
sns.swarmplot(x=["cat", "cat", "dog", "dog"], y=[2, 3, 4, 5])

Before Example: You have categorical data but no way to show individual data points clearly. ๐Ÿค”

Data: Categories: ["cat", "dog"], Values: [2, 3, 4, 5]

After Example: With swarmplot, you display individual data points without overlap! ๐Ÿ

Output: A swarm plot showing the distribution of points.

Challenge: ๐ŸŒŸ Try using hue to color-code points based on an additional variable.


15. Point Plot (seaborn.pointplot) ๐Ÿ“

Boilerplate Code:

import seaborn as sns

Use Case: Create a point plot to visualize the relationship between categorical data and numerical data. ๐Ÿ“

Goal: Show mean values of categories with confidence intervals. ๐ŸŽฏ

Sample Code:

# Example data
sns.pointplot(x=["A", "B", "C"], y=[4, 5, 6])

Before Example: You have categorical data but no way to visualize the mean values across categories. ๐Ÿค”

Data: Categories: ["A", "B", "C"], Values: [4, 5, 6]

After Example: With pointplot, the mean values are visualized with error bars! ๐Ÿ“

Output: A point plot showing means with error bars.

Challenge: ๐ŸŒŸ Try adding hue to compare means across different categories.


16. LM Plot (seaborn.lmplot) ๐Ÿงฎ

Boilerplate Code:

import seaborn as sns

Use Case: Create an lmplot to visualize linear relationships with multiple facets. ๐Ÿงฎ

Goal: Combine regression lines and scatter plots with facet grids. ๐ŸŽฏ

Sample Code:

# Load example data
df = sns.load_dataset("tips")

# Create lmplot
sns.lmplot(x="total_bill", y="tip", hue="sex", data=df)

Before Example: You want to visualize both scatter plots and linear regression with multiple subsets of data. ๐Ÿค”

Data: Tips dataset with total bill and tip variables.

After Example: With lmplot, the linear regression lines and scatter plots are displayed! ๐Ÿงฎ

Output: A scatter plot with regression lines based on gender.

Challenge: ๐ŸŒŸ Try using col to split the plot by time of day.


17. Strip Plot (seaborn.stripplot) ๐ŸŸก

Boilerplate Code:

import seaborn as sns

Use Case: Create a strip plot to display individual data points with some jitter. ๐ŸŸก

Goal: Show the distribution of data points along a categorical axis with slight jitter. ๐ŸŽฏ

Sample Code:

# Example data
sns.stripplot(x=["cat", "dog", "dog", "cat"], y=[2, 3, 4, 5])

Before Example: You have categorical data and want to display individual points, but they overlap. ๐Ÿค”

Data: Categories: ["cat", "dog"], Values: [2, 3, 4, 5]

After Example: With stripplot, you can add jitter to make individual points clearer! ๐ŸŸก

Output: A strip plot with jittered data points.

Challenge: ๐ŸŒŸ Try using hue to differentiate points based on another category.


18. PairGrid (seaborn.PairGrid) ๐Ÿ”—

Boilerplate Code:

import seaborn as sns

Use Case: Create a pair grid to visualize pairwise relationships between variables using different plots. ๐Ÿ”—

Goal: Combine multiple plot types (scatter, histograms, etc.) into a grid for deeper analysis. ๐ŸŽฏ

Sample Code:

# Load example data
df = sns.load_dataset("iris")

# Create PairGrid
g = sns.PairGrid(df)
g.map_diag(sns.histplot)
g.map_offdiag(sns.scatterplot)

Before Example: You have multiple variables and want to use different plot types to explore their relationships. ๐Ÿค”

Data: Iris dataset with multiple numerical variables.

After Example: With PairGrid, you combine scatter plots, histograms, and more in one view! ๐Ÿ”—

Output: A grid of plots for each pair of variables.

Challenge: ๐ŸŒŸ Try mapping different plot types to diagonal and off-diagonal elements for a more detailed analysis.


19. Joint Grid (seaborn.JointGrid) โš™๏ธ

Boilerplate Code:

import seaborn as sns

Use Case: Create a joint grid to visualize the relationship between two variables using multiple plots. โš™๏ธ

Goal: Create a custom plot that combines scatter plots, KDE, or histograms. ๐ŸŽฏ

Sample Code:

# Example data
g = sns.JointGrid(x=[1, 2, 3], y=[4, 5, 6])
g.plot(sns

.scatterplot, sns.histplot)

Before Example: You want to customize the combination of scatter and distribution plots. ๐Ÿค”

Data: x = [1, 2, 3], y = [4, 5, 6]

After Example: With JointGrid, you customize how different plots are displayed together! โš™๏ธ

Output: A combined scatter plot and histogram.

Challenge: ๐ŸŒŸ Try using plot_kws to add custom styles to the plots.


20. Heatmap with Annotations (seaborn.heatmap) ๐Ÿ”ข

Boilerplate Code:

import seaborn as sns

Use Case: Create a heatmap with annotations to visualize a matrix of numbers with text annotations. ๐Ÿ”ข

Goal: Display numerical data as color-coded intensity and show the exact values. ๐ŸŽฏ

Sample Code:

# Example data
import numpy as np
data = np.array([[1, 2], [3, 4]])
sns.heatmap(data, annot=True)

Before Example: You have a matrix of numbers but want both a color map and the exact values. ๐Ÿค”

Data: [[1, 2], [3, 4]]

After Example: With heatmap and annotations, the data is color-coded and labeled! ๐Ÿ”ข

Output: A heatmap showing data values with annotations.

Challenge: ๐ŸŒŸ Try changing the fmt argument to display different formats for annotations.


Seaborn documentation

https://seaborn.pydata.org/generated/seaborn.FacetGrid.html

0
Subscribe to my newsletter

Read articles from Anix Lynch directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anix Lynch
Anix Lynch