{ "cells": [ { "cell_type": "markdown", "id": "d68a1604-1d1c-4a04-95eb-ca0e0f9d4da1", "metadata": {}, "source": [ "# Waterfall Plot\n", "\n", "A waterfall plot shows the cumulative effect of sequentially introduced positive or negative values.\n", "\n", "To use it, you need to import the 'bistro' module." ] }, { "cell_type": "code", "execution_count": 1, "id": "7b926beb-92f0-405e-8425-cef02e49285e", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.bistro import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "67f97700-ee2e-4a27-8eee-296b4307c79e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "0a5272f5-a594-4ce6-84e5-941528ac115d", "metadata": {}, "outputs": [], "source": [ "data = {\n", " \"Accounts\": [\"Product revenue\", \"Services revenue\", \"Fixed costs\", \"Variable costs\"],\n", " \"Values\": [830_000, 290_000, -360_000, -150_000],\n", "}" ] }, { "cell_type": "markdown", "id": "1858f51f-2da3-4f37-be27-f659db1e9752", "metadata": {}, "source": [ "## Default View" ] }, { "cell_type": "code", "execution_count": 4, "id": "ec40036a-b909-4f7f-8324-231dc2197885", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "waterfall_plot(data, \"Accounts\", \"Values\")" ] }, { "cell_type": "markdown", "id": "6ab3c679-ae24-474b-b651-b208d8b9b839", "metadata": {}, "source": [ "## Improved View" ] }, { "cell_type": "code", "execution_count": 5, "id": "5c6f023a-fc3a-4e10-a147-3c019c40aeab", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "waterfall_plot(data, \"Accounts\", \"Values\", \\\n", " size=.75, alpha=.15, width=.8, total_title=\"Profit\", \\\n", " hline=element_line(linetype='solid', size=1), \\\n", " connector=element_line(linetype='dotted'), \\\n", " label=element_text(size=10, family=\"Courier\", face='bold', color='flow_type'), \\\n", " label_format=\"$,.1~s\") + \\\n", " scale_y_continuous(name=\"Values\", format=\"$,.1~s\") + \\\n", " ggtitle(\"Company Profit (in USD)\") + \\\n", " ggsize(1000, 500) + \\\n", " theme_minimal() + \\\n", " theme(plot_title=element_text(size=20, face='bold', hjust=.5))" ] }, { "cell_type": "markdown", "id": "a184b578-1ad1-44cc-8b5b-b7e4272a74cc", "metadata": {}, "source": [ "## Additional Parameters" ] }, { "cell_type": "markdown", "id": "a1769b18-83c1-4188-bd10-1bbd04824e76", "metadata": {}, "source": [ "### `measure` and `group`" ] }, { "cell_type": "code", "execution_count": 6, "id": "3e168567-84d9-4fe7-8378-f1994ba9ae91", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame({\n", " \"Company\": [\"Badgersoft\"] * 7 + [\"AIlien Co.\"] * 7,\n", " \"Accounts\": [\"initial\", \"revenue\", \"costs\", \"Q1\", \"revenue\", \"costs\", \"Q2\"] * 2,\n", " \"Values\": [200, 200, -100, None, 250, -100, None, \\\n", " 150, 50, -100, None, 100, -100, None],\n", " \"Measure\": ['absolute', 'relative', 'relative', 'total', 'relative', 'relative', 'total'] * 2,\n", "})\n", "\n", "waterfall_plot(df, \"Accounts\", \"Values\", measure=\"Measure\", group=\"Company\") + \\\n", " facet_grid(x=\"Company\", scales='free_x')" ] }, { "cell_type": "markdown", "id": "2bbe2c0c-1ac5-484b-b500-11a021618da2", "metadata": {}, "source": [ "### `calc_total`\n", "\n", "`calc_total=False` disables the calculation of the total.\n", "\n", "If the `measure` serie is specified however, the `calc_total` setting has no effect." ] }, { "cell_type": "code", "execution_count": 7, "id": "dcdec1d7-b879-43cc-ab00-4e51515e27b6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " waterfall_plot(data, \"Accounts\", \"Values\", calc_total=False),\n", " waterfall_plot(df[df[\"Company\"] == \"Badgersoft\"], \"Accounts\", \"Values\", measure=\"Measure\", calc_total=False),\n", "])" ] }, { "cell_type": "markdown", "id": "e674cb66-a316-4d12-a69f-d934e229a9b0", "metadata": {}, "source": [ "### Tooltips\n", "\n", "Tooltips for relative and absolute measures should be specified independently." ] }, { "cell_type": "code", "execution_count": 8, "id": "5e4b5e87-ddf4-44a9-be53-76854fc70e85", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "relative_tooltips = layer_tooltips().title(\"Account: @..xlabel..\")\\\n", " .format(\"@..initial..\", \" $,.3~s\")\\\n", " .format(\"@..value..\", \" $,.3~s\")\\\n", " .line(\"@{..flow_type..}d from @..initial.. to @..value..\")\\\n", " .disable_splitting()\n", "absolute_tooltips = 'none'\n", "\n", "gggrid([\n", " waterfall_plot(data, \"Accounts\", \"Values\", relative_tooltips='detailed', absolute_tooltips='detailed') + ggtitle(\"'detailed' tooltips\"),\n", " waterfall_plot(data, \"Accounts\", \"Values\", relative_tooltips=relative_tooltips, absolute_tooltips=absolute_tooltips) + ggtitle(\"Custom tooltips\"),\n", "])" ] }, { "cell_type": "markdown", "id": "d784f373-fd3d-4bcf-9c1d-ca2c526902e2", "metadata": {}, "source": [ "### `sorted_value`" ] }, { "cell_type": "code", "execution_count": 9, "id": "e6a1bf56-2bba-4bc2-8e14-53e0ed2a405d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "waterfall_plot(data, \"Accounts\", \"Values\", sorted_value=True)" ] }, { "cell_type": "markdown", "id": "df771bf1-bb42-4694-bcb2-81be2df25097", "metadata": {}, "source": [ "### `threshold`/`max_values`" ] }, { "cell_type": "code", "execution_count": 10, "id": "efd0ce82-8f0f-4b38-8836-892d1f84345f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " waterfall_plot(data, \"Accounts\", \"Values\") + ggtitle(\"Default\"),\n", " waterfall_plot(data, \"Accounts\", \"Values\", threshold=300_000) + ggtitle(\"Specified threshold\"),\n", " waterfall_plot(data, \"Accounts\", \"Values\", max_values=2) + ggtitle(\"Specified max_values\"),\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.19" } }, "nbformat": 4, "nbformat_minor": 5 }