{ "cells": [ { "cell_type": "markdown", "id": "2ac0e74d", "metadata": {}, "source": [ "# The `gggrid()` Function\n", "\n", "\n", "Use the `gggrid()` function to combine several plots on one figure, organized in a regular grid." ] }, { "cell_type": "markdown", "id": "957a3809", "metadata": {}, "source": [ "1. [Simple Triptych](#1.-Simple-Triptych)\n", "2. [Simple 2 X 2 Grid](#2.-Simple-2-X-2-Grid) \n", "3. [Re-arranged 2 X 2 Grid](#3.-Re-arranged-2-X-2-Grid) \n", "4. [Align Inner Areas of Plots](#4.-Align-Inner-Areas-of-Plots) \n", "5. [Adjust Cell Sizes](#5.-Adjust-Cell-Sizes) \n", "6. [Hierarchical Grids](#6.-Hierarchical-Grids) \n", "\n", " 6.1 [Two Plots Arranged in Column](#6.1-Two-Plots-Arranged-in-Column)\n", "\n", " 6.2 [Two Grids on One Figure](#6.2-Two-Grids-on-One-Figure)\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "8ab5b1ed", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "b0b9825c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()\n", "LetsPlot.set_theme(theme_bw())" ] }, { "cell_type": "code", "execution_count": 3, "id": "728faab1", "metadata": {}, "outputs": [], "source": [ "np.random.seed(123)\n", "N = 200\n", "data = {\n", " \"sepal length\": np.random.normal(0, 1, N),\n", " \"sepal width\": np.random.normal(100000, 10000, N),\n", "}\n", "scatter = ggplot(data) + geom_point(aes(\"sepal length\", \"sepal width\"), color=\"black\", alpha=0.3, size=5)\n", "density = ggplot(data) + geom_density(aes(\"sepal length\"), color=\"black\", fill=\"black\", alpha=0.1, size=1)\n", "\n", "blank_x_axis = theme(axis_title_x=\"blank\", axis_text_x=\"blank\", axis_ticks_x=\"blank\")\n", "box = ggplot(data) + geom_boxplot(aes(y=\"sepal width\"), fill=\"black\", alpha=0.1, size=1) + blank_x_axis" ] }, { "cell_type": "markdown", "id": "97da6e7e", "metadata": {}, "source": [ "#### 1. Simple Triptych" ] }, { "cell_type": "code", "execution_count": 4, "id": "8d7ab533", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([scatter, density, box])" ] }, { "cell_type": "markdown", "id": "0a779d1e", "metadata": {}, "source": [ "#### 2. Simple 2 X 2 Grid" ] }, { "cell_type": "code", "execution_count": 5, "id": "e82fd38c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([scatter, density, box], ncol=2) + ggsize(500, 500)" ] }, { "cell_type": "markdown", "id": "5e8643b7", "metadata": {}, "source": [ "#### 3. Re-arranged 2 X 2 Grid\n", "\n", "Use value `None` to fill-in empty cells." ] }, { "cell_type": "code", "execution_count": 6, "id": "7db969c3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plots = [density, None, \n", " scatter, box]\n", "\n", "gggrid(plots, ncol=2) + ggsize(500, 500)" ] }, { "cell_type": "markdown", "id": "852eec69", "metadata": {}, "source": [ "#### 4. Align Inner Areas of Plots" ] }, { "cell_type": "code", "execution_count": 7, "id": "5e6d519e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(plots, ncol=2, align=True) + ggsize(500, 500)" ] }, { "cell_type": "markdown", "id": "a7c6504f", "metadata": {}, "source": [ "#### 5. Adjust Cell Sizes\n", "\n", "Use `widths` and `heights` parameters to specify relative width/height of each \n", "column/row of the grid." ] }, { "cell_type": "code", "execution_count": 8, "id": "e9e0998d", "metadata": {}, "outputs": [], "source": [ "# Choose better settings for axis on the \"density\" and \"box\" plots.\n", "better_density = density + theme(axis_title=\"blank\", axis_text=\"blank\", axis_ticks=\"blank\")\n", "better_box = box + scale_y_continuous(position=\"right\")\n", "\n", "better_plots = [better_density, None, \n", " scatter, better_box]" ] }, { "cell_type": "code", "execution_count": 9, "id": "e6b6e310", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "triplet = gggrid(better_plots, ncol=2, align=True,\n", " widths=[1, 0.5],\n", " heights=[0.4, 1],\n", " hspace=0, vspace=0) + ggsize(700, 500)\n", "\n", "triplet" ] }, { "cell_type": "markdown", "id": "b8877904", "metadata": {}, "source": [ "#### 6. Hierarchical Grids\n", "\n", "You can insert one grid in a cell of another, bigger grid to create more complex figure.\n", "\n", "Let's add another two plots on the right-hand side." ] }, { "cell_type": "markdown", "id": "9e01a07b", "metadata": {}, "source": [ "##### 6.1 Two Plots Arranged in Column" ] }, { "cell_type": "code", "execution_count": 10, "id": "b6c2a496", "metadata": {}, "outputs": [], "source": [ "from lets_plot.bistro import *" ] }, { "cell_type": "code", "execution_count": 11, "id": "52961daf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qqs = [\n", " qq_plot(data, sample=\"sepal width\", size=5, color=\"gray\", alpha=0.2) + ggtitle(\"Sepal Width\"),\n", " qq_plot(data, sample=\"sepal length\", size=5, color=\"gray\", alpha=0.2) + ggtitle(\"Sepal Length\"),\n", "] \n", "\n", "qq_size = ggsize(300, 300 * 1.8)\n", "qq_pair = gggrid(qqs, ncol=1, align=True) + qq_size\n", "qq_pair" ] }, { "cell_type": "markdown", "id": "bfcc71b9", "metadata": {}, "source": [ "##### 6.2 Two Grids on One Figure\n", "\n", "Optionally, use `fit=False` to preserve aspect ratios of sub-grids. " ] }, { "cell_type": "code", "execution_count": 12, "id": "055a6744", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Remove axis titles to declutter the figure.\n", "better_qqs = [p + theme(axis_title=\"blank\") for p in qqs]\n", "better_qq_pair = gggrid(better_qqs, ncol=1, align=True) + qq_size\n", "\n", "# Create a grid of two grids.\n", "composite = [triplet, better_qq_pair]\n", "gggrid(composite, widths=[1, 0.5], fit=False) + ggsize(800, 800/2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.15" } }, "nbformat": 4, "nbformat_minor": 5 }