{ "cells": [ { "cell_type": "markdown", "id": "1f5bafcb-0750-4d39-9be5-3736baae9eb0", "metadata": {}, "source": [ "# Combining `waterfall_plot()` with Other Geometry Layers\n", "\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" ] }, { "cell_type": "code", "execution_count": 1, "id": "80a0cec9-510a-48c3-8412-1f5b867b74fd", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.bistro import waterfall_plot" ] }, { "cell_type": "code", "execution_count": 2, "id": "39eabe85-6deb-4cef-b4cd-6ecb37e0b822", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "347c9a4e-bcdd-446c-b275-a9bf5a030f50", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Waterfall data\n", "df = pd.DataFrame({\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", "# Background layer and its data\n", "quarter_data = {\n", " \"period_start\": [0.5, 4.5],\n", " \"period_end\": [4.5, 8.5],\n", " \"ai_introduced\": [False, True],\n", "}\n", "quarter_layer = geom_band(\n", " aes(\n", " xmin=\"period_start\",\n", " xmax=\"period_end\",\n", " paint_a=\"ai_introduced\"\n", " ),\n", " data=quarter_data,\n", " alpha=0.2,\n", " # We use \"paint_a\" to color the bands based on a separate category (e.g., quarters),\n", " # so they have their own color palette independent from the waterfalls\n", " fill_by=\"paint_a\",\n", " color_by=\"paint_a\"\n", ")\n", "\n", "# Foreground layers and their data\n", "quarter_label_data = {\n", " \"name\": [\"Q1\", \"Q2\", \"Q3\", \"Q4\"],\n", " \"x\": [1.5, 3.5, 5.5, 7.5],\n", " \"y\": [750] * 4\n", "}\n", "quarter_ai_status_data = {\n", " \"text\": [\"Before AI\\nintroduction\", \"After AI\\nintroduction\"],\n", " \"x\": [2.5, 6.5],\n", " \"y\": [100, 100],\n", "}\n", "text_layers = geom_text(aes(x=\"x\", y=\"y\", label=\"name\"), data=quarter_label_data, size=8) + \\\n", " geom_text(aes(x=\"x\", y=\"y\", label=\"text\"), data=quarter_ai_status_data, size=12)\n", "\n", "# Whole plot\n", "(waterfall_plot(df, \"Accounts\", \"Values\", measure=\"Measure\",\n", " background_layers=quarter_layer) # Background layer\n", " + text_layers # Foreground layers\n", " + scale_hue(\"paint_a\", guide=\"none\") # Color for the background layer (bands)\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.9.23" } }, "nbformat": 4, "nbformat_minor": 5 }