Search Knowledge

© 2026 LIBREUNI PROJECT

Python for Scientific Computing / Data Visualization Deep Dive

Seaborn: Statistical Data Visualization

High-Level Statistical Plotting

If Matplotlib is the “foundational” layer, Seaborn is the “sophisticated” layer. Built on top of Matplotlib, it integrates deeply with Pandas and automates many complex statistical visualization tasks.

Why Seaborn?

  1. DataFrame Integration: You can pass column names directly as strings.
  2. Beautiful Defaults: Better color palettes and styles out of the box.
  3. Statistical Aggregation: It automatically calculates confidence intervals and means for you.

Exploring Relationships with relplot

relplot is the primary entry point for visualizing the relationship between two variables.

python
1 
2import seaborn as sns
3import matplotlib.pyplot as plt
4import pandas as pd
5import numpy as np
6 
7# Create synthetic tips-like data
8df = pd.DataFrame({
9 'total_bill': np.random.uniform(10, 50, 100),
10 'tip': np.random.uniform(1, 10, 100),
11 'smoker': np.random.choice(['Yes', 'No'], 100),
12 'day': np.random.choice(['Thur', 'Fri', 'Sat', 'Sun'], 100)
13})
14 
15# Add a correlation
16df['tip'] = df['total_bill'] * 0.15 + np.random.normal(0, 1, 100)
17 
18sns.set_theme(style="ticks")
19 
20# Plot relationship by smoker status and day
21g = sns.relplot(
22 data=df, x="total_bill", y="tip",
23 col="day", hue="smoker", style="smoker",
24 kind="scatter"
25)
26 
27plt.show()
28 

Categorical Data and Distributions

When dealing with categories, catplot and displot are your best friends.

python
1 
2import seaborn as sns
3import matplotlib.pyplot as plt
4import pandas as pd
5import numpy as np
6 
7data = pd.DataFrame({
8 'Value': np.concatenate([np.random.normal(0, 1, 100), np.random.normal(2, 0.5, 100)]),
9 'Group': ['A']*100 + ['B']*100
10})
11 
12fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
13 
14# Violin plot
15sns.violinplot(data=data, x="Group", y="Value", ax=ax1, palette="muted")
16ax1.set_title("Distribution by Group (Violin)")
17 
18# ECDF (Empirical Cumulative Distribution Function)
19sns.ecdfplot(data=data, x="Value", hue="Group", ax=ax2)
20ax2.set_title("CDF Comparison")
21 
22plt.show()
23 

Regression Models with lmplot

Seaborn can perform linear regression right inside the visualization.

python
1 
2import seaborn as sns
3import matplotlib.pyplot as plt
4 
5df = sns.load_dataset("anscombe").query("dataset == 'I'")
6 
7sns.lmplot(x="x", y="y", data=df, ci=95, scatter_kws={"s": 80})
8plt.title("Statistical Regression Analysis")
9plt.show()
10 

Heatmaps and Matrix Plots

Heatmaps are essential for visualizing correlation matrices.

python
1 
2import seaborn as sns
3import matplotlib.pyplot as plt
4import numpy as np
5import pandas as pd
6 
7# Correlation matrix for synthetic features
8data = np.random.rand(10, 10)
9cols = [f'Feat_{i}' for i in range(10)]
10df = pd.DataFrame(data, columns=cols)
11corr = df.corr()
12 
13plt.figure(figsize=(10, 8))
14sns.heatmap(corr, annot=True, cmap='coolwarm', fmt=".2f")
15plt.title("Feature Correlation Matrix")
16plt.show()
17 

In the final lesson of this module, we will discuss the principles of scientific storytelling and how to choose the right chart for your data.