Seaborn
Seaborn
Introduction to Seaborn
Main Features:
Simplifies common tasks such as visualizing distributions, relationships, and categorical data.
Importing Seaborn
Before we start using Seaborn, we need to import it along with Matplotlib for
plotting.
import seaborn as sns
import matplotlib.pyplot as plt
Seaborn Themes
Seaborn allows you to customize the appearance of your plots easily with built-in
themes. There are several preset themes:
# Apply a theme
sns.set_theme(style='darkgrid')
You can also just plot the KDE without the histogram to estimate the probability
density function of a continuous variable.
# KDE plot
sns.kdeplot(tips['total_bill'])
plt.show()
3.2. Pairplot
The pairplot function creates pairwise scatter plots of all numerical columns in the
dataset. This is useful for examining relationships across the dataset.
# Pairplot with hue for categorical distinction
sns.pairplot(tips, hue='sex')
plt.show()
A bar plot shows the relationship between a categorical variable and a continuous
variable by aggregating the data.
# Bar plot showing the average tip by day
sns.barplot(x='day', y='tip', data=tips)
plt.show()
You can change the estimator from the default (mean) to other statistical functions
like sum, median, or count.
# Bar plot showing the total bill by day (sum)
sns.barplot(x='day', y='total_bill', estimator=sum, data=tips)
plt.show()
A box plot is a great way to visualize the distribution of data and identify outliers.
# Box plot comparing the distribution of tips by day
sns.boxplot(x='day', y='tip', data=tips)
plt.show()
Box plots can also show multiple variables by adding the hue parameter.
# Box plot with hue
sns.boxplot(x='day', y='tip', hue='sex', data=tips)
plt.show()
4.3. Violin Plot
A violin plot combines the features of a box plot and a KDE plot, giving a deeper
understanding of the data distribution.
# Violin plot comparing the distribution of tips by day
sns.violinplot(x='day', y='tip', data=tips)
plt.show()
5. Heatmaps
# Create a heatmap
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.show()
6. Regression Plots
The lmplot function allows you to plot linear regressions between variables.
# Linear regression plot between total_bill and tip
sns.lmplot(x='total_bill', y='tip', data=tips)
plt.show()
7. Customizing Plots
You can add titles, labels, and adjust other elements using Matplotlib functions.
# Add a title and labels
sns.scatterplot(x='total_bill', y='tip', data=tips)
plt.title('Scatter Plot of Total Bill vs. Tip')
plt.xlabel('Total Bill ($)')
plt.ylabel('Tip ($)')
plt.show()
Seaborn plots can be resized using the plt.figure function from Matplotlib.
# Resize plot
plt.figure(figsize=(10, 6))
sns.scatterplot(x='total_bill', y='tip', data=tips)
plt.show()
8. Matrix plots are a special type of plot in Seaborn that display data in a grid
format, often used for visualizing relationships between variables in a dataset or
showing correlations between them. Seaborn offers several matrix plot functions,
such as heatmaps and clustermaps, which help with analyzing patterns, trends, and
correlations.
Heatmap
What is a Heatmap?
A heatmap is a 2D plot where each cell of the matrix contains a color representing
the value of a variable. It's commonly used to visualize correlation matrices, or any
matrix-like data (like a confusion matrix in machine learning).
Basic Syntax
Explanation:
2. Clustermap
What is a Clustermap?
A clustermap is an enhanced heatmap that clusters both rows and columns using
hierarchical clustering algorithms. It helps in identifying patterns in data and
grouping similar rows or columns together.
Basic Syntax
# Load the dataset
iris = sns.load_dataset('iris')
Explanation:
3. Pairplot
What is a Pairplot?
Though not strictly a matrix plot, the pairplot is a very common visualization used
to examine pairwise relationships between variables in a dataset. It plots all
possible scatter plots between numerical columns, along with histograms or KDE
plots for the diagonal.
Basic Syntax
# Load the iris dataset
iris = sns.load_dataset('iris')
# Create a pairplot
sns.pairplot(iris, hue='species')
Explanation: