0
0
Matplotlibdata~15 mins

Small multiples (facet grid) in Matplotlib - Deep Dive

Choose your learning style9 modes available
Overview - Small multiples (facet grid)
What is it?
Small multiples, also called facet grids, are a way to show many similar charts side by side. Each chart shows a slice of the data based on a category or variable. This helps compare patterns across groups easily without mixing all data in one plot. It is like having many mini charts arranged in a grid for clear comparison.
Why it matters
Without small multiples, comparing groups means cluttered charts or many separate plots that are hard to read together. Small multiples let you see differences and similarities quickly, making data stories clearer. This saves time and reduces mistakes when analyzing or presenting data.
Where it fits
Before learning small multiples, you should know basic plotting with matplotlib and how to handle data with pandas. After this, you can explore advanced visualization libraries like seaborn or plotly that build on these ideas for easier and prettier plots.
Mental Model
Core Idea
Small multiples split data into many small charts arranged in a grid, each showing a part of the data for easy side-by-side comparison.
Think of it like...
Imagine a photo album where each page shows pictures from a different trip. Instead of mixing all photos in one page, each page groups them by trip, making it easy to see what happened where.
┌─────────────┬─────────────┬─────────────┐
│ Chart for 1 │ Chart for 2 │ Chart for 3 │
├─────────────┼─────────────┼─────────────┤
│ Chart for 4 │ Chart for 5 │ Chart for 6 │
└─────────────┴─────────────┴─────────────┘
Build-Up - 7 Steps
1
FoundationUnderstanding basic matplotlib plots
🤔
Concept: Learn how to create simple plots using matplotlib to visualize data.
Use matplotlib's pyplot module to create a basic line or scatter plot. For example, plot x and y lists using plt.plot(x, y) and show the plot with plt.show().
Result
A window or inline display showing a simple chart with the data points connected or scattered.
Knowing how to make a basic plot is essential before splitting data into multiple charts.
2
FoundationGrouping data with pandas
🤔
Concept: Learn to organize data by categories using pandas groupby to prepare for faceting.
Load data into a pandas DataFrame. Use df.groupby('category') to split data into groups. Each group can be accessed and plotted separately.
Result
Data is divided into subsets based on category values, ready for individual plotting.
Grouping data is the first step to creating small multiples, as each plot shows one group.
3
IntermediateCreating multiple subplots manually
🤔Before reading on: do you think you can create multiple plots in one figure by calling plt.plot multiple times or do you need a different approach? Commit to your answer.
Concept: Learn to create a grid of subplots using plt.subplots to place multiple charts in one figure.
Use fig, axes = plt.subplots(rows, cols) to create a grid. Loop over groups and axes to plot each group's data on its own subplot. Adjust layout with plt.tight_layout().
Result
A figure window showing multiple small charts arranged in a grid, each representing a data group.
Understanding subplot grids is key to arranging small multiples neatly and comparably.
4
IntermediateAutomating small multiples with loops
🤔Before reading on: do you think manually coding each subplot is efficient for many groups, or is looping better? Commit to your answer.
Concept: Use loops to automate plotting each group on its subplot, making code scalable for many categories.
Iterate over grouped data and subplot axes simultaneously. Plot each group's data inside the loop. This avoids repetitive code and handles any number of groups.
Result
Code that easily creates small multiples for any number of groups without manual repetition.
Looping over groups and axes makes small multiples practical for real datasets with many categories.
5
IntermediateCustomizing axes for clarity
🤔
Concept: Learn to set titles, labels, and limits on each subplot for better readability.
Inside the plotting loop, set each subplot's title to the group name. Use set_xlabel and set_ylabel for axis labels. Use set_xlim and set_ylim to keep scales consistent across plots.
Result
Each small chart is clearly labeled and comparable because axes are consistent.
Consistent axes and clear labels help viewers compare patterns across small multiples easily.
6
AdvancedUsing matplotlib's GridSpec for flexible layouts
🤔Before reading on: do you think plt.subplots is enough for all grid layouts, or does GridSpec offer more control? Commit to your answer.
Concept: GridSpec allows precise control over subplot sizes and positions beyond uniform grids.
Import GridSpec from matplotlib.gridspec. Create a GridSpec object with desired rows and columns. Assign subplots to specific grid cells with different sizes or spans.
Result
A figure with a customized grid layout where some plots can be larger or arranged irregularly.
GridSpec enables advanced small multiples layouts for complex data presentations.
7
ExpertIntegrating small multiples with pandas and matplotlib
🤔Before reading on: do you think pandas plotting can replace manual matplotlib loops for small multiples? Commit to your answer.
Concept: Pandas DataFrame has a built-in plot method with a 'subplots' option to create small multiples easily.
Use df.plot(subplots=True, layout=(rows, cols), sharex=True, sharey=True) to generate small multiples automatically. Customize with matplotlib parameters as needed.
Result
Quick generation of small multiples directly from pandas without manual subplot management.
Leveraging pandas plotting simplifies small multiples creation, saving time and reducing errors.
Under the Hood
Matplotlib creates a figure object that holds one or more axes (plots). Each axes is a container for a single chart. When using small multiples, matplotlib arranges multiple axes in a grid inside one figure. Each axes independently draws its data but shares the figure's canvas. This allows consistent styling and layout control.
Why designed this way?
Matplotlib was designed to mimic MATLAB's plotting style, focusing on flexibility and control. The figure-axes model separates the container (figure) from the content (axes), enabling complex layouts like small multiples. Alternatives like seaborn automate this but with less control. This design balances power and customization.
Figure (canvas)
┌─────────────────────────────┐
│ ┌───────┐ ┌───────┐ ┌───────┐ │
│ │ Axes1 │ │ Axes2 │ │ Axes3 │ │
│ └───────┘ └───────┘ └───────┘ │
│ ┌───────┐ ┌───────┐ ┌───────┐ │
│ │ Axes4 │ │ Axes5 │ │ Axes6 │ │
│ └───────┘ └───────┘ └───────┘ │
└─────────────────────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Do you think small multiples always share the same axis scales by default? Commit to yes or no.
Common Belief:Small multiples automatically share the same x and y axis scales to make comparison easy.
Tap to reveal reality
Reality:By default, each subplot has independent axis scales unless explicitly set to share axes.
Why it matters:If scales differ, visual comparison can be misleading, making some groups look bigger or smaller unfairly.
Quick: Do you think small multiples are only useful for categorical data? Commit to yes or no.
Common Belief:Small multiples only work when splitting data by categories like groups or classes.
Tap to reveal reality
Reality:Small multiples can also split data by ranges of continuous variables by binning or discretizing.
Why it matters:Limiting small multiples to categories reduces their usefulness for exploring continuous data patterns.
Quick: Do you think creating small multiples with matplotlib is always simpler than using specialized libraries? Commit to yes or no.
Common Belief:Matplotlib is the easiest way to create small multiples because it is the base plotting library.
Tap to reveal reality
Reality:Matplotlib requires more manual setup for small multiples; libraries like seaborn or plotly automate this better.
Why it matters:Not knowing this can lead to unnecessarily complex code and slower development.
Quick: Do you think plotting all data in one plot is better than small multiples for clarity? Commit to yes or no.
Common Belief:Combining all data in one plot is clearer than splitting into many small charts.
Tap to reveal reality
Reality:Overplotting in one chart can hide group differences; small multiples reveal patterns clearly by separation.
Why it matters:Ignoring small multiples can cause misinterpretation due to clutter and overlapping data.
Expert Zone
1
Small multiples require careful axis scaling choices; sharing axes improves comparison but can hide group-specific ranges.
2
Using GridSpec allows mixing small multiples with larger summary plots in one figure for richer storytelling.
3
Pandas plotting with subplots is convenient but less flexible than manual matplotlib control for fine-tuning.
When NOT to use
Avoid small multiples when the number of groups is very large (e.g., hundreds), as the grid becomes unreadable. Instead, use interactive plots with filtering or summary statistics. Also, for very complex multidimensional data, consider dimensionality reduction or heatmaps.
Production Patterns
Professionals use small multiples in dashboards to compare metrics across regions or time periods. They combine small multiples with consistent color schemes and annotations for clarity. Automated report generation pipelines often create small multiples for routine monitoring.
Connections
Dashboard design
Small multiples are a core technique used in dashboards to display multiple related metrics side by side.
Understanding small multiples helps design dashboards that communicate complex data clearly and efficiently.
Human visual perception
Small multiples leverage our brain's ability to compare similar shapes and patterns side by side quickly.
Knowing how humans perceive visual information explains why small multiples are effective for spotting differences.
Photography contact sheets
Both show many small images arranged in grids to compare and select the best ones.
This cross-domain link shows how arranging many small views aids decision-making in different fields.
Common Pitfalls
#1Using different axis scales across small multiples without intention.
Wrong approach:for ax, group in zip(axes.flatten(), groups): ax.plot(group['x'], group['y']) plt.show()
Correct approach:for ax, group in zip(axes.flatten(), groups): ax.plot(group['x'], group['y']) ax.set_xlim(global_x_min, global_x_max) ax.set_ylim(global_y_min, global_y_max) plt.show()
Root cause:Not setting axis limits explicitly causes matplotlib to auto-scale each subplot independently.
#2Manually coding each subplot for many groups, leading to repetitive code.
Wrong approach:plt.subplot(2,2,1) plt.plot(group1['x'], group1['y']) plt.subplot(2,2,2) plt.plot(group2['x'], group2['y']) # repeats for all groups
Correct approach:fig, axes = plt.subplots(2, 2) for ax, group in zip(axes.flatten(), groups): ax.plot(group['x'], group['y'])
Root cause:Not using loops or subplot grids causes inefficient and error-prone code.
#3Plotting too many small multiples in one figure making them unreadable.
Wrong approach:fig, axes = plt.subplots(10, 10) for ax, group in zip(axes.flatten(), groups): ax.plot(group['x'], group['y'])
Correct approach:Limit number of subplots or use interactive filtering tools instead of huge grids.
Root cause:Ignoring human visual limits and screen space constraints leads to cluttered, useless plots.
Key Takeaways
Small multiples arrange many small charts in a grid to compare data groups side by side clearly.
Creating small multiples requires grouping data and plotting each group on its own subplot with consistent axes.
Matplotlib's subplot grids and loops are essential tools to build small multiples efficiently.
Careful axis scaling and labeling are critical to avoid misleading comparisons across plots.
While powerful, small multiples have limits and should be combined with other visualization techniques for large or complex data.