{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `ggbunch()`\n", "\n", "`ggbunch()` allows to show a collection of plots on one figure. Each plot in the collection can have arbitrary location and size. There is no automatic layout inside the bunch." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:48.300913Z", "iopub.status.busy": "2025-11-05T13:48:48.300788Z", "iopub.status.idle": "2025-11-05T13:48:48.304935Z", "shell.execute_reply": "2025-11-05T13:48:48.304679Z" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:48.305818Z", "iopub.status.busy": "2025-11-05T13:48:48.305704Z", "iopub.status.idle": "2025-11-05T13:48:48.307696Z", "shell.execute_reply": "2025-11-05T13:48:48.307504Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:48.320833Z", "iopub.status.busy": "2025-11-05T13:48:48.320753Z", "iopub.status.idle": "2025-11-05T13:48:48.322051Z", "shell.execute_reply": "2025-11-05T13:48:48.321874Z" } }, "outputs": [], "source": [ "np.random.seed(42)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:48.322701Z", "iopub.status.busy": "2025-11-05T13:48:48.322633Z", "iopub.status.idle": "2025-11-05T13:48:48.324346Z", "shell.execute_reply": "2025-11-05T13:48:48.324166Z" } }, "outputs": [], "source": [ "cov=[[1, 0],\n", " [0, 1]]\n", "x, y = np.random.multivariate_normal(mean=[0,0], cov=cov, size=400).T\n", "\n", "data = dict(\n", " x = x,\n", " y = y\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View this data as a scatter plot and as a histogram" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:48.325066Z", "iopub.status.busy": "2025-11-05T13:48:48.324992Z", "iopub.status.idle": "2025-11-05T13:48:48.358050Z", "shell.execute_reply": "2025-11-05T13:48:48.357851Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(data) + ggsize(600, 200)\n", "\n", "scatter = p + geom_point(aes('x', 'y'), color='black', alpha=.4)\n", "scatter" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:48.358961Z", "iopub.status.busy": "2025-11-05T13:48:48.358884Z", "iopub.status.idle": "2025-11-05T13:48:48.361884Z", "shell.execute_reply": "2025-11-05T13:48:48.361706Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "histogram = p + geom_histogram(aes('x', y = '..count..'), fill='dark_magenta')\n", "histogram" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Combine both plots in one figure" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:48.362619Z", "iopub.status.busy": "2025-11-05T13:48:48.362548Z", "iopub.status.idle": "2025-11-05T13:48:48.367722Z", "shell.execute_reply": "2025-11-05T13:48:48.367548Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Set scale X limits manually because of computed automatically\n", "# the scale used by each plot would be slightly different\n", "# and the stacked plots wouldn't be aligned.\n", "scale_x = scale_x_continuous(limits=[-3.5, 3.5])\n", "ggbunch(\n", " [\n", " histogram + scale_x,\n", " scatter + scale_x\n", " ],\n", " [\n", " (0, 0, 1, .5),\n", " (0, .5, 1, .5),\n", " ]\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adjust visuals of the bunch figure" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:48.368453Z", "iopub.status.busy": "2025-11-05T13:48:48.368384Z", "iopub.status.idle": "2025-11-05T13:48:48.369776Z", "shell.execute_reply": "2025-11-05T13:48:48.369599Z" } }, "outputs": [], "source": [ "upper_theme = theme(axis_title_x='blank', axis_ticks_x='blank', axis_line='blank', \\\n", " panel_grid='blank')\n", "lower_theme = theme(axis_text_x='blank', axis_ticks_x='blank', axis_line='blank')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:48.370438Z", "iopub.status.busy": "2025-11-05T13:48:48.370368Z", "iopub.status.idle": "2025-11-05T13:48:48.375659Z", "shell.execute_reply": "2025-11-05T13:48:48.375486Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggbunch(\n", " [\n", " histogram + upper_theme + scale_x,\n", " scatter + lower_theme + scale_x\n", " ],\n", " [\n", " (0, 0, 1, .5),\n", " (0, .5, 1, .5),\n", " ]\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Move the regions\n", "\n", "You can additionally move the bottom plot up a bit with the last two parameters of the tuple responsible for the layout of the region." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:48.376383Z", "iopub.status.busy": "2025-11-05T13:48:48.376314Z", "iopub.status.idle": "2025-11-05T13:48:48.381509Z", "shell.execute_reply": "2025-11-05T13:48:48.381339Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggbunch(\n", " [\n", " histogram + upper_theme + scale_x,\n", " scatter + lower_theme + scale_x\n", " ],\n", " [\n", " (0, 0, 1, .5),\n", " (0, .5, 1, .5, 0, -12), # Move up 12 pixels\n", " ]\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adjust plot sizes\n", "\n", "The width and height of the plots within `ggbunch()` are given in relative units, but you can change the size of the whole `ggbunch()` plot with `ggsize()`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:48.382208Z", "iopub.status.busy": "2025-11-05T13:48:48.382138Z", "iopub.status.idle": "2025-11-05T13:48:48.387387Z", "shell.execute_reply": "2025-11-05T13:48:48.387214Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggbunch(\n", " [\n", " histogram + upper_theme + scale_x,\n", " scatter + lower_theme + scale_x\n", " ],\n", " [\n", " (0, 0, 1, .5),\n", " (0, .5, 1, .5, 0, -12),\n", " ]\n", ") + ggsize(600, 400)" ] } ], "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.10.13" } }, "nbformat": 4, "nbformat_minor": 4 }