{ "cells": [ { "cell_type": "markdown", "id": "8ff4b4d6-d79a-4752-8916-ac0faaf0c77e", "metadata": {}, "source": [ "# Theme-driven Facet Spacing Options\n", "\n", "You can control spacing in multipanel (faceted) plots. \n", "\n", "* `strip_spacing` - the gap between the facet strip (title bar) and the plot panel. By default, there is no gap.\n", "* `panel_spacing` - the gap between plot panels in both directions (default: 10).\n", "* `strip_spacing_x`, `strip_spacing_y`, `panel_spacing_x`, `panel_spacing_y` - directional overrides to control spacing horizontally (suffix `_x`) or vertically (suffix `_y`) only.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "b48c0618-ee9f-4463-bc18-8b7be6623f94", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "757b7464-165e-44ab-b9f1-614458b3d627", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "a0256dde-c804-45e8-8978-94506d4617bd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(234, 12)\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
34audia42.020084auto(av)f2130pcompact
45audia42.819996auto(l5)f1626pcompact
\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 \n", "3 4 audi a4 2.0 2008 4 auto(av) f 21 30 \n", "4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 \n", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 p compact \n", "3 p compact \n", "4 p compact " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 4, "id": "9a212f11-8622-4aa9-ad5f-d326d70f23f8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Base plot\n", "\n", "p = ggplot(df, aes(x=\"cty\", y=\"hwy\")) + \\\n", " geom_point(aes(color=\"class\"), shape=21) + \\\n", " theme_light() + \\\n", " facet_grid(x=\"cyl\", y=\"year\")\n", "p" ] }, { "cell_type": "code", "execution_count": 5, "id": "91c034cb-6ebd-4f00-b893-1465c7292571", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Increase the gap between facet strip and panel\n", "\n", "p + theme(strip_spacing=10)" ] }, { "cell_type": "code", "execution_count": 6, "id": "00768730-de32-4914-be79-42ccd80ae902", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Make panels closer together for a more compact view\n", "\n", "p + theme(panel_spacing=3)" ] }, { "cell_type": "code", "execution_count": 7, "id": "69dd1335-06f0-4c64-a963-75f671349fe5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Fine-tune horizontal and vertical spacing independently\n", "\n", "p + theme(strip_spacing_y=5, panel_spacing_x=1, panel_spacing_y=5)" ] } ], "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.18" } }, "nbformat": 4, "nbformat_minor": 5 }