The gggrid() Function#
Use the gggrid() function to combine several plots on one figure, organized in a regular grid.
import numpy as np
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
LetsPlot.set_theme(theme_bw())
np.random.seed(123)
N = 200
data = {
"sepal length": np.random.normal(0, 1, N),
"sepal width": np.random.normal(100000, 10000, N),
}
scatter = ggplot(data) + geom_point(aes("sepal length", "sepal width"), color="black", alpha=0.3, size=5)
density = ggplot(data) + geom_density(aes("sepal length"), color="black", fill="black", alpha=0.1, size=1)
blank_x_axis = theme(axis_title_x="blank", axis_text_x="blank", axis_ticks_x="blank")
box = ggplot(data) + geom_boxplot(aes(y="sepal width"), fill="black", alpha=0.1, size=1) + blank_x_axis
1. Simple Triptych#
gggrid([scatter, density, box])
2. Simple 2 X 2 Grid#
gggrid([scatter, density, box], ncol=2) + ggsize(500, 500)
3. Re-arranged 2 X 2 Grid#
Use value None to fill-in empty cells.
plots = [density, None,
scatter, box]
gggrid(plots, ncol=2) + ggsize(500, 500)
4. Align Inner Areas of Plots#
gggrid(plots, ncol=2, align=True) + ggsize(500, 500)
5. Adjust Cell Sizes#
Use widths and heights parameters to specify relative width/height of each
column/row of the grid.
# Choose better settings for axis on the "density" and "box" plots.
better_density = density + theme(axis_title="blank", axis_text="blank", axis_ticks="blank")
better_box = box + scale_y_continuous(position="right")
better_plots = [better_density, None,
scatter, better_box]
triplet = gggrid(better_plots, ncol=2, align=True,
widths=[1, 0.5],
heights=[0.4, 1],
hspace=0, vspace=0) + ggsize(700, 500)
triplet
6. Hierarchical Grids#
You can insert one grid in a cell of another, bigger grid to create more complex figure.
Let’s add another two plots on the right-hand side.
6.1 Two Plots Arranged in Column#
from lets_plot.bistro import *
qqs = [
qq_plot(data, sample="sepal width", size=5, color="gray", alpha=0.2) + ggtitle("Sepal Width"),
qq_plot(data, sample="sepal length", size=5, color="gray", alpha=0.2) + ggtitle("Sepal Length"),
]
qq_size = ggsize(300, 300 * 1.8)
qq_pair = gggrid(qqs, ncol=1, align=True) + qq_size
qq_pair
6.2 Two Grids on One Figure#
Optionally, use fit=False to preserve aspect ratios of sub-grids.
# Remove axis titles to declutter the figure.
better_qqs = [p + theme(axis_title="blank") for p in qqs]
better_qq_pair = gggrid(better_qqs, ncol=1, align=True) + qq_size
# Create a grid of two grids.
composite = [triplet, better_qq_pair]
gggrid(composite, widths=[1, 0.5], fit=False) + ggsize(800, 800/2)