DEV Community

Cover image for Advanced Seaborn: Facets, Correlations & Pro Styling
Nivesh Bansal
Nivesh Bansal

Posted on

Advanced Seaborn: Facets, Correlations & Pro Styling

Day 2 — Advanced Seaborn
Seaborn is one of the most popular Python libraries for data visualization.

If you already know the basics (scatter plots, bar plots, etc.), this guide will help you level up.

We’ll cover:

  • Faceting & small multiples
  • Correlations & heatmaps
  • Ordered/time trends
  • Styling & palettes
  • Legends, annotations, and saving plots
  • Common pitfalls and pro tips

Let’s get started 👇


Faceting & Small Multiples

Faceting means splitting your data into subplots by category.

This makes comparisons easier without cluttering a single plot.

Common Methods

Method Description
FacetGrid Low-level, flexible faceting
catplot / relplot High-level figure-level plots with built-in facets

Example 1: Bar plot faceted by smoker

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.catplot(
    data=tips, x="day", y="tip", hue="sex", col="smoker", kind="bar"
)
plt.suptitle("Tips by Day (Faceted by Smoker)", y=1.02)
plt.show()
Enter fullscreen mode Exit fullscreen mode

Example 2: Scatter plot with facets

sns.relplot(
    data=tips, x="total_bill", y="tip", hue="sex", col="time", kind="scatter"
)
plt.show()
Enter fullscreen mode Exit fullscreen mode

Correlations & Heatmaps

Heatmaps summarize numeric relationships visually.
They’re perfect for spotting patterns and strengths of correlation.
Repo link — GitHub

Example 1: Simple correlation heatmap

corr = tips[["total_bill", "tip", "size"]].corr()

sns.heatmap(corr, annot=True, fmt=".2f", cmap="coolwarm", square=True)
plt.title("Correlation (tips)")
plt.show()
Enter fullscreen mode Exit fullscreen mode

Example 2: Clustered heatmap (groups similar variables)

sns.clustermap(corr, annot=True, fmt=".2f", cmap="coolwarm")
plt.show()
Enter fullscreen mode Exit fullscreen mode

👉** Quick Tip:**

  • Values near +1 or -1 = strong relationship
  • Values near 0 = weak or no relationship

Time/Ordered Trends

For ordered variables (like time, size, or sequence), line plots are ideal.

Example: Average tip by table size

avg = tips.groupby("size", as_index=False)["tip"].mean()

sns.lineplot(data=avg, x="size", y="tip")
plt.title("Average Tip by Party Size")
plt.show()
Enter fullscreen mode Exit fullscreen mode

Styling, Palettes & Layout

Good styling = professional plots.

Use sns.set_theme() to apply global settings.

Options

Parameter Examples
style "white", "whitegrid", "dark", "darkgrid", "ticks"
palette "deep", "muted", "bright", color_palette()
context "notebook", "talk", "paper", "poster"

Example: Styled scatter plot

sns.set_theme(style="whitegrid", context="talk", palette="deep")

ax = sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex")
ax.set_title("Tips vs Total Bill")
ax.set_xlabel("Total Bill ($)")
ax.set_ylabel("Tip ($)")

sns.despine()
plt.tight_layout()
plt.show()
Enter fullscreen mode Exit fullscreen mode

Legends, Annotations & Saving

Always label plots clearly and save in high resolution for reports.

Example: Annotated regression plot

ax = sns.regplot(data=tips, x="total_bill", y="tip")
ax.annotate("Higher tips with higher bills",
            xy=(40,7), xytext=(25,8.5),
            arrowprops=dict(arrowstyle="->", color="white"))

# Remove legend if not needed
ax.legend_.remove() if ax.legend_ else None  

plt.tight_layout()
plt.savefig("tips_scatter.png", dpi=300, bbox_inches="tight", transparent=True)
plt.show()
Enter fullscreen mode Exit fullscreen mode

Figure- vs Axes-level Functions

Type Examples When to Use
Axes-level scatterplot, lineplot, histplot, boxplot, violinplot, heatmap, regplot Manage subplots manually
Figure-level relplot, catplot, jointplot, pairplot, lmplot Automatic grids/facets

👉 Rule of thumb:

  • Axes-level = more control, manual layouts
  • Figure-level = quick grids, less code

Common Pitfalls (and Fixes)

Problem Solution
Too many overlapping points Use alpha=0.5, hexbin, or kdeplot
Default labels are unclear Always set titles, labels, legends
Comparing groups poorly Use violinplot/boxplot + stripplot instead of bar means
Colors inconsistent Stick to one palette across related charts

Repo link — GitHub

Top comments (0)