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
colorrowhas 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
| Parameter | Description |
|---|---|
| data | Pandas DataFrame with your data |
| 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 (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.