Facets¶
Faceting creates small multiples - the same plot repeated for subsets of your data.
In [1]:
Copied!
import pandas as pd
import numpy as np
from ggplotly import *
# Sample data
np.random.seed(42)
df = pd.DataFrame({
'x': np.random.randn(200),
'y': np.random.randn(200),
'category': np.random.choice(['A', 'B', 'C', 'D'], 200),
'group': np.random.choice(['Group 1', 'Group 2'], 200),
'value': np.random.rand(200) * 10
})
import pandas as pd
import numpy as np
from ggplotly import *
# Sample data
np.random.seed(42)
df = pd.DataFrame({
'x': np.random.randn(200),
'y': np.random.randn(200),
'category': np.random.choice(['A', 'B', 'C', 'D'], 200),
'group': np.random.choice(['Group 1', 'Group 2'], 200),
'value': np.random.rand(200) * 10
})
facet_wrap¶
Wraps a 1D sequence of panels into 2D.
In [2]:
Copied!
# Basic faceting by one variable
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_wrap('category')
# Basic faceting by one variable
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_wrap('category')
Out[2]:
Controlling Layout¶
In [3]:
Copied!
# Specify number of columns
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_wrap('category', ncol=2)
# Specify number of columns
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_wrap('category', ncol=2)
Out[3]:
Free Scales¶
By default, all panels share the same axis scales. Allow them to vary:
In [4]:
Copied!
# Free both axes
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_wrap('category', scales='free')
# Free both axes
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_wrap('category', scales='free')
Out[4]:
In [5]:
Copied!
# Free only y-axis
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_wrap('category', scales='free_y')
# Free only y-axis
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_wrap('category', scales='free_y')
Out[5]:
facet_grid¶
Creates a 2D grid based on two variables.
In [6]:
Copied!
# Rows by one variable, columns by another
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_grid(rows='category', cols='group')
# Rows by one variable, columns by another
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_grid(rows='category', cols='group')
Out[6]:
Grid Layout¶
Use '.' as a placeholder when you only want rows or columns:
In [7]:
Copied!
# Only rows
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_grid(rows='category', cols='.')
# Only rows
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_grid(rows='category', cols='.')
Out[7]:
In [8]:
Copied!
# Only columns
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_grid(rows='.', cols='group')
# Only columns
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_grid(rows='.', cols='group')
Out[8]:
Free Scales in Grid¶
In [9]:
Copied!
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_grid(rows='category', cols='group', scales='free')
ggplot(df, aes(x='x', y='y')) + geom_point() + facet_grid(rows='category', cols='group', scales='free')
Out[9]:
In [10]:
Copied!
ggplot(df, aes(x='value')) + geom_histogram(bins=20) + facet_wrap('group')
ggplot(df, aes(x='value')) + geom_histogram(bins=20) + facet_wrap('group')
Out[10]:
Scatter by Category¶
In [11]:
Copied!
ggplot(df, aes(x='x', y='y', color='group')) + geom_point() + facet_wrap('category', ncol=2)
ggplot(df, aes(x='x', y='y', color='group')) + geom_point() + facet_wrap('category', ncol=2)
Out[11]:
Facet Reference¶
| Function | Description |
|---|---|
facet_wrap(facets) |
Wrap 1D into 2D grid |
facet_grid(rows, cols) |
2D grid of panels |
facet_wrap Parameters¶
| Parameter | Description |
|---|---|
facets |
Column name to facet by |
ncol |
Number of columns |
nrow |
Number of rows |
scales |
'fixed', 'free', 'free_x', 'free_y' |
facet_grid Parameters¶
| Parameter | Description |
|---|---|
rows |
Column name for rows (use '.' for none) |
cols |
Column name for columns (use '.' for none) |
scales |
'fixed', 'free', 'free_x', 'free_y' |