0
0
Data-analysis-pythonHow-ToBeginner ยท 4 min read

How to Create FacetGrid in Seaborn with Python

Use seaborn.FacetGrid to create a grid of plots based on subsets of your data. Initialize it with your DataFrame and specify the column(s) to facet by, then map a plotting function like sns.scatterplot to draw the plots.
๐Ÿ“

Syntax

The basic syntax to create a FacetGrid is:

  • FacetGrid(data, col=None, row=None, hue=None, height=5, aspect=1)
  • data: Your dataset as a pandas DataFrame.
  • col: Column name to create columns of plots.
  • row: Column name to create rows of plots.
  • hue: Column name to color points differently.
  • height: Height of each facet in inches.
  • aspect: Aspect ratio of each facet (width = height * aspect).

After creating the grid, use .map() to specify the plotting function and variables.

python
import seaborn as sns

grid = sns.FacetGrid(data, col='column_name', row='another_column', hue='color_column', height=4, aspect=1)
grid.map(sns.scatterplot, 'x_variable', 'y_variable')
grid.add_legend()
๐Ÿ’ป

Example

This example shows how to create a FacetGrid using the built-in tips dataset. It creates scatter plots of total bill vs tip, faceted by day of the week.

python
import seaborn as sns
import matplotlib.pyplot as plt

# Load example dataset
tips = sns.load_dataset('tips')

# Create FacetGrid with columns by 'day'
grid = sns.FacetGrid(tips, col='day', height=4, aspect=1)

# Map scatterplot to grid
grid.map(sns.scatterplot, 'total_bill', 'tip')

# Add legend and show plot
grid.add_legend()
plt.show()
Output
A window with 4 scatter plots side by side, each labeled with a day of the week, showing total bill on x-axis and tip on y-axis.
โš ๏ธ

Common Pitfalls

  • Not calling .map() after creating the FacetGrid will result in an empty grid.
  • Using incorrect column names causes errors or empty plots.
  • For categorical variables, ensure the column used in col or row has a manageable number of unique values to avoid too many plots.
  • For some plot types, you may need to use .map_dataframe() instead of .map() to pass the full data subset.
python
import seaborn as sns

# Wrong: missing .map() call
# grid = sns.FacetGrid(tips, col='day')
# plt.show()  # This shows empty plots

# Right: include .map() to plot
# grid = sns.FacetGrid(tips, col='day')
# grid.map(sns.scatterplot, 'total_bill', 'tip')
# plt.show()
๐Ÿ“Š

Quick Reference

ParameterDescription
dataPandas DataFrame with your data
colColumn name to create columns of plots
rowColumn name to create rows of plots
hueColumn name to color points differently
heightHeight of each facet in inches
aspectAspect ratio (width = height * aspect)
.map(func, x, y)Apply plotting function to variables
.add_legend()Add legend for hue colors
โœ…

Key Takeaways

Use seaborn.FacetGrid to create multiple plots based on subsets of your data.
Always call .map() with a plotting function after creating the FacetGrid.
Facet by columns, rows, or hue to organize your plots clearly.
Check column names carefully to avoid errors or empty plots.
Use manageable categories for faceting to keep plots readable.