{ "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": "2024-04-17T07:34:39.796313Z", "iopub.status.busy": "2024-04-17T07:34:39.796230Z", "iopub.status.idle": "2024-04-17T07:34:40.117275Z", "shell.execute_reply": "2024-04-17T07:34:40.116930Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:40.131326Z", "iopub.status.busy": "2024-04-17T07:34:40.131166Z", "iopub.status.idle": "2024-04-17T07:34:40.132700Z", "shell.execute_reply": "2024-04-17T07:34:40.132526Z" } }, "outputs": [], "source": [ "np.random.seed(42)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:40.133926Z", "iopub.status.busy": "2024-04-17T07:34:40.133778Z", "iopub.status.idle": "2024-04-17T07:34:40.135685Z", "shell.execute_reply": "2024-04-17T07:34:40.135494Z" } }, "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": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:40.136701Z", "iopub.status.busy": "2024-04-17T07:34:40.136585Z", "iopub.status.idle": "2024-04-17T07:34:40.170261Z", "shell.execute_reply": "2024-04-17T07:34:40.169904Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "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": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:40.171674Z", "iopub.status.busy": "2024-04-17T07:34:40.171352Z", "iopub.status.idle": "2024-04-17T07:34:40.174312Z", "shell.execute_reply": "2024-04-17T07:34:40.174139Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "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": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:40.175380Z", "iopub.status.busy": "2024-04-17T07:34:40.175261Z", "iopub.status.idle": "2024-04-17T07:34:40.179806Z", "shell.execute_reply": "2024-04-17T07:34:40.179637Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "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", "bunch = GGBunch()\n", "bunch.add_plot(histogram + scale_x, 0, 0)\n", "bunch.add_plot(scatter + scale_x, 0, 200)\n", "bunch.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adjust visuals of the bunch figure" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:40.180860Z", "iopub.status.busy": "2024-04-17T07:34:40.180747Z", "iopub.status.idle": "2024-04-17T07:34:40.182165Z", "shell.execute_reply": "2024-04-17T07:34:40.181990Z" } }, "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": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:40.183130Z", "iopub.status.busy": "2024-04-17T07:34:40.182995Z", "iopub.status.idle": "2024-04-17T07:34:40.187354Z", "shell.execute_reply": "2024-04-17T07:34:40.187179Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch1 = GGBunch()\n", "bunch1.add_plot(histogram + upper_theme + scale_x, 0, 0)\n", "bunch1.add_plot(scatter + lower_theme + scale_x, 0, 200)\n", "bunch1.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adjust plot sizes\n", "\n", "*add_plot()* method has two more (optional) parameters: *width* and *height*.\n", "\n", "This values will override plot size earlier defined via *ggsize()* function." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:40.188318Z", "iopub.status.busy": "2024-04-17T07:34:40.188202Z", "iopub.status.idle": "2024-04-17T07:34:40.192710Z", "shell.execute_reply": "2024-04-17T07:34:40.192538Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch2 = GGBunch()\n", "bunch2.add_plot(histogram + upper_theme + scale_x, 0, 0, 600, 100)\n", "bunch2.add_plot(scatter + lower_theme + scale_x, 0, 100, 600, 300)\n", "bunch2.show()" ] } ], "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": 2 }