{ "cells": [ { "cell_type": "markdown", "id": "cf021754", "metadata": {}, "source": [ "# Applying Common Theme to a Plot Group\n", "\n", "You can add a **common theme** to an antire group of plots (i.e. `gggrid()`) to have \\\n", "each subplot in the group inherited this theme.\n", "\n", "Howether, a **theme** added to an individual subplot will override settings in the **common theme**." ] }, { "cell_type": "code", "execution_count": 1, "id": "ce848ad8", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "a342e22d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "8ac75c66", "metadata": {}, "outputs": [], "source": [ "# Line\n", "x = np.linspace(-4 * np.pi, 4 * np.pi, 100)\n", "line_data = {\n", " 'x': x, \n", " 'y': np.sin(x)\n", "} \n", "line = ggplot(line_data, aes(x='x', y='y')) + geom_line() + ggtitle(\"Line\")\n", "\n", "# Bars\n", "np.random.seed(37)\n", "bar_data = {'x': np.random.randint(10, size=100)}\n", "bar = ggplot(bar_data, aes(x='x')) + geom_bar() + ggtitle(\"Barchart\")\n", " \n", "# Boxplot\n", "box_data = {\n", " 'x': np.random.choice(['a', 'b', 'c'], size=100), \n", " 'y': np.random.normal(size=100)\n", "}\n", "box = ggplot(box_data, aes(x='x', y='y')) + geom_boxplot() + ggtitle(\"Boxplot\")\n", "\n", "# Pie\n", "pie_data = {\n", " 'name' : ['rock', 'paper', 'scissors'],\n", " 'slice': [1, 3, 3]\n", "}\n", "pie = ggplot(pie_data) + geom_pie(aes(fill='name', slice='slice'),\n", " stat='identity',\n", " size=0.5, size_unit=\"x\"\n", " )\n", "\n", "# Grid\n", "grid = gggrid(\n", " [line, bar, box, pie],\n", " ncol=2\n", ") + ggsize(700, 400)" ] }, { "cell_type": "markdown", "id": "aedd9918", "metadata": {}, "source": [ "#### 1. A Group without Custom Theme Settings" ] }, { "cell_type": "code", "execution_count": 4, "id": "90778ae0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grid" ] }, { "cell_type": "markdown", "id": "132be13a", "metadata": {}, "source": [ "#### 2. Use Common Theme to Remove All Titles in Subplots" ] }, { "cell_type": "code", "execution_count": 5, "id": "1fd45d60", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "no_titles = theme(axis_title=\"blank\", plot_title=\"blank\")\n", "\n", "grid + no_titles" ] }, { "cell_type": "markdown", "id": "8b68dfc1", "metadata": {}, "source": [ "#### 3. Add \"Common Flawor\" to All Subplots" ] }, { "cell_type": "code", "execution_count": 6, "id": "ea485171", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "no_titles_dark = no_titles + flavor_solarized_dark()\n", "\n", "grid + no_titles_dark" ] }, { "cell_type": "markdown", "id": "2533c8b1", "metadata": {}, "source": [ "#### 4. Customize the Pie Chart Theme Individually" ] }, { "cell_type": "code", "execution_count": 7, "id": "bc9d2173", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pie_theme = theme_void() + flavor_solarized_dark()\n", "\n", "gggrid([line, bar, box, \n", " pie + pie_theme],\n", " ncol=2\n", ") + ggsize(700, 400) + no_titles_dark" ] }, { "cell_type": "markdown", "id": "afd5ab44", "metadata": {}, "source": [ "#### 5. Add Common Margins and Border" ] }, { "cell_type": "code", "execution_count": 8, "id": "f29620f0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "no_titles_dark_margins = no_titles_dark + \\\n", "theme(plot_margin=margin(40, 40, 40, 40), plot_background=element_rect(color=\"orange\", size=20))\n", "\n", "gggrid([line, bar, box, \n", " pie + pie_theme],\n", " ncol=2\n", ") + ggsize(700, 400) + no_titles_dark_margins\n" ] } ], "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 }