{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b937bced-017c-45aa-b9ee-ca1e5910cda2", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.bistro import *\n", "import polars as pl" ] }, { "cell_type": "code", "execution_count": 2, "id": "e039bf65-d878-451f-8d81-49d1adcf47e3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "ae21768e-04ef-4021-89c2-76e3ac8257b1", "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": "73c06577-4cb9-4a66-8940-b90ce7c38d3e", "metadata": {}, "source": [ "#### Hide specific labels" ] }, { "cell_type": "code", "execution_count": 4, "id": "dfaac0d7-2fe0-4a2a-919d-12d281249db8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "waterfall_plot(data, \"Accounts\", \"Values\", relative_labels=\"none\") " ] }, { "cell_type": "markdown", "id": "7648f4bd-e857-4017-ba28-f33d1aec23c7", "metadata": {}, "source": [ "#### The color from the label is overridden by the theme." ] }, { "cell_type": "code", "execution_count": 5, "id": "9342252e-4faa-4384-8aa5-85eb246bcb2a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "waterfall_plot(data, \"Accounts\", \"Values\", label=element_text(color=\"black\")) + theme(label_text=element_text(color=\"red\")) " ] }, { "cell_type": "markdown", "id": "39150195-2b43-4d5f-9f11-1f7a658013dd", "metadata": {}, "source": [ "#### Issues with Waterfall Plot customizability #1341" ] }, { "cell_type": "code", "execution_count": 6, "id": "28ef8892-8102-4324-ad50-014b0ac3554b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.setup_html()\n", "stages = ('Initiated Registration', 'B4 Redirection', 'Reached Redirection', '@ Redirection', 'Redirected', 'After Redirection', 'Total Registered')\n", "data = {\n", " 'stage': stages,\n", " 'count': [598, -264, 334, -4, 330, -85, 245],\n", " 'measure': ['absolute', 'relative', 'total', 'relative', 'total', 'relative', 'total'],\n", "}\n", "df = pl.DataFrame(data)\n", "# Add each step's/stage's percentage of initial count:\n", "df = df.with_columns(\n", " ((pl.col('count')/pl.lit(598.) * pl.lit(100.)).abs().round()\n", " .cast(pl.String) + pl.lit(\"%\"))\n", " .alias('pct')\n", " )\n", "\n", "water = (waterfall_plot(df, 'stage', 'count', measure='measure', relative_labels=layer_labels().line(\"@pct\")) \n", " + scale_y_continuous(breaks=list(range(0, 601, 100)))\n", " + coord_cartesian(ylim=(0, 650))\n", ")\n", "water" ] }, { "cell_type": "markdown", "id": "a72ff9cf-7fb5-41bc-bd87-4ab02fe34902", "metadata": {}, "source": [ "#### Multiline" ] }, { "cell_type": "code", "execution_count": 7, "id": "3c773eee-a2c2-4325-a4ee-7b672708e094", "metadata": {}, "outputs": [], "source": [ "data = {\n", " 'stage': [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"ToTaL\"],\n", " 'count': [598, -264, 156, -4, 330, -85, 245],\n", " 'pct': [0, 44.14, 46.71, 1.81, 67.9, 10.41, 0],\n", " 'measure': ['absolute', 'relative', 'relative', 'relative', 'relative', 'relative', 'total'],\n", "}" ] }, { "cell_type": "code", "execution_count": 8, "id": "a6acf176-681d-46dc-8058-95b12ae03d4e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "waterfall_plot(data, 'stage', 'count', measure='measure', relative_labels=layer_labels().line(\"@count\").line(\"@pct%\").format(\"pct\", \".0f\"))" ] }, { "cell_type": "code", "execution_count": null, "id": "85f208d2-35cc-425b-a35e-65947be65d28", "metadata": {}, "outputs": [], "source": [] } ], "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.20" } }, "nbformat": 4, "nbformat_minor": 5 }