{ "cells": [ { "cell_type": "markdown", "id": "1f5bafcb-0750-4d39-9be5-3736baae9eb0", "metadata": {}, "source": [ "# Additional layers for waterfall_plot\n", "This notebook demonstrates how to enrich a waterfall plot with background and foreground layers. Foreground layers can be added using the regular `+` operator. To add background layers, use the new `background_layers` property.\n", "\n", "Limitations:\n", "- Layers must provide their own data\n", "- Data coordinates are expected to be numeric\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "80a0cec9-510a-48c3-8412-1f5b867b74fd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "from lets_plot.bistro import waterfall_plot\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "LetsPlot.setup_html() " ] }, { "cell_type": "code", "execution_count": 2, "id": "347c9a4e-bcdd-446c-b275-a9bf5a030f50", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Base plot data\n", "df = pd.DataFrame({\n", " \"Company\": [\"Badgersoft\"] * 10,\n", " \"Accounts\": [\"initial\", \"revenue\", \"costs\", \"revenue\", \"costs\", \"revenue\", \"costs\", \"revenue\", \"costs\", \"total\"],\n", " \"Values\": [200, 200, -150, 250, -170, 150, -50, 280, -25, None],\n", " \"Measure\": ['absolute', 'relative', 'relative', 'relative', 'relative', 'relative', 'relative', 'relative',\n", " 'relative', 'total'],\n", "})\n", "\n", "\n", "# Additional layers data\n", "quoter_text_data = {\n", " 'name': ['Q1', 'Q2', 'Q3', 'Q4'],\n", " 'x': [1, 3, 5, 7],\n", " 'y': [725] * 4\n", "}\n", "\n", "ai_status_data = {\n", " 'text': ['Before AI\\nIntroduction', 'After AI\\nIntroduction'],\n", " 'period_start': [0.5, 4.5],\n", " 'period_end': [4.5, 8.5],\n", " 'ai_introduced': [False, True],\n", " 'text_x': [2.5, 6.5],\n", " 'text_y': [120, 120]\n", "}\n", "\n", "waterfall_plot(df, \"Accounts\", \"Values\", measure=\"Measure\", group=\"Company\",\n", " background_layers=\n", " geom_band(\n", " aes(\n", " xmin='period_start', \n", " xmax='period_end', \n", " paint_a='ai_introduced' # paint_a to keep the default waterfall colors\n", " ), \n", " data=ai_status_data,\n", " alpha=0.2,\n", " fill_by='paint_a',\n", " color_by='paint_a'\n", " )\n", " ) + \\\n", " geom_text(aes(x='x', y='y', label='name'), data=quoter_text_data, size=8) + \\\n", " geom_text(aes(x='text_x', y='text_y', label='text'), data=ai_status_data, size=12) + \\\n", " scale_hue('paint_a', guide='none') + \\\n", " ggsize(750, 450) + \\\n", " ggtitle(\"Waterfall with additional layers\")" ] } ], "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.12.8" } }, "nbformat": 4, "nbformat_minor": 5 }