#!/usr/bin/env python # coding: utf-8 # # The `gggrid()` Function # # # Use the `gggrid()` function to combine several plots on one figure, organized in a regular grid. # 1. [Simple Triptych](#1.-Simple-Triptych) # 2. [Simple 2 X 2 Grid](#2.-Simple-2-X-2-Grid) # 3. [Re-arranged 2 X 2 Grid](#3.-Re-arranged-2-X-2-Grid) # 4. [Align Inner Areas of Plots](#4.-Align-Inner-Areas-of-Plots) # 5. [Adjust Cell Sizes](#5.-Adjust-Cell-Sizes) # 6. [Hierarchical Grids](#6.-Hierarchical-Grids) # # 6.1 [Two Plots Arranged in Column](#6.1-Two-Plots-Arranged-in-Column) # # 6.2 [Two Grids on One Figure](#6.2-Two-Grids-on-One-Figure) # # In[1]: import numpy as np from lets_plot import * # In[2]: LetsPlot.setup_html() LetsPlot.set_theme(theme_bw()) # In[3]: 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 # In[4]: gggrid([scatter, density, box]) # #### 2. Simple 2 X 2 Grid # In[5]: gggrid([scatter, density, box], ncol=2) + ggsize(500, 500) # #### 3. Re-arranged 2 X 2 Grid # # Use value `None` to fill-in empty cells. # In[6]: plots = [density, None, scatter, box] gggrid(plots, ncol=2) + ggsize(500, 500) # #### 4. Align Inner Areas of Plots # In[7]: 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. # In[8]: # 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] # In[9]: 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 # In[10]: from lets_plot.bistro import * # In[11]: 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. # In[12]: # 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)