{ "cells": [ { "cell_type": "markdown", "id": "8023e47d-62e8-4192-98d2-5728e53bd208", "metadata": {}, "source": [ "# Waterfall Chart" ] }, { "cell_type": "code", "execution_count": 1, "id": "fc5ec06c-8e83-4bed-9b92-a75e548c0045", "metadata": {}, "outputs": [], "source": [ "from lets_plot import *\n", "from lets_plot.bistro import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "d28d3d4d-e8cd-44e6-b52c-c46b985dc52c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "166ccf3a-a333-48dc-910a-e790ec869c65", "metadata": {}, "outputs": [], "source": [ "data1 = dict(\n", " x = [\"A\", \"B\", \"C\", \"D\", \"E\"],\n", " y = [300, -100, -400, 300, 200],\n", ")\n", "\n", "data2 = dict(\n", " x = [\"A\", \"B\", \"C\", \"D\", \"T1\", \"A\", \"B\", \"C\", \"D\", \"T2\"],\n", " y = [100, 100, -300, 500, None, -200, 300, 100, -300, 0.0],\n", " m = ['relative', 'relative', 'relative', 'relative', 'total', 'relative', 'relative', 'relative', 'relative', 'total'],\n", ")\n", "\n", "data3 = dict(\n", " x = [\"A\", \"B\", \"C\", \"D\", \"E\", \"A\", \"B\", \"C\", \"D\", \"T\"],\n", " y = [100, 100, -300, 500, 300, -200, 300, 100, -300, 0.0],\n", " m = ['absolute', 'relative', 'relative', 'relative', 'absolute', 'relative', 'relative', 'relative', 'relative', 'total'],\n", ")" ] }, { "cell_type": "markdown", "id": "8c8ff80a-7239-45de-95f2-aff92ec80663", "metadata": {}, "source": [ "## Default" ] }, { "cell_type": "code", "execution_count": 4, "id": "e7c6897f-528c-4c53-972d-0366e87d2343", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " waterfall_plot(data1, 'x', 'y') + ggtitle(\"Without measure\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m') + ggtitle(\"With measure\", \"Without absolute\"),\n", " waterfall_plot(data3, 'x', 'y', measure='m') + ggtitle(\"With measure\", \"With absolute\"),\n", "])" ] }, { "cell_type": "markdown", "id": "8e466d64-26fe-4cc4-8779-ade4144a3071", "metadata": {}, "source": [ "## Parameters" ] }, { "cell_type": "markdown", "id": "7028d937-c877-484f-9e55-ea430e916eff", "metadata": {}, "source": [ "### Aesthetics" ] }, { "cell_type": "code", "execution_count": 5, "id": "2a13ecdb-fb38-4602-bd3a-e1dcf57aaa47", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# color\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', size=1, color=\"magenta\") + ggtitle(\"Without measure\"),\n", " waterfall_plot(data1, 'x', 'y', size=1, color='flow_type', fill=\"lightgrey\") + ggtitle(\"Without measure\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', size=1, color=\"magenta\") + ggtitle(\"With measure\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', size=1, color='flow_type', fill=\"lightgrey\") + ggtitle(\"With measure\"),\n", "], ncol=2)" ] }, { "cell_type": "code", "execution_count": 6, "id": "89b4dd39-10f7-4acf-8ef1-7c17b0c355e8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# fill\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', fill=\"magenta\") + ggtitle(\"Without measure\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', fill=\"magenta\") + ggtitle(\"With measure\"),\n", "])" ] }, { "cell_type": "code", "execution_count": 7, "id": "06806c57-12b3-4c87-9e45-6e5c68b39623", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# size\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', size=2) + ggtitle(\"Without measure\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', size=2) + ggtitle(\"With measure\"),\n", "])" ] }, { "cell_type": "code", "execution_count": 8, "id": "3b220383-1fc7-4738-b0ba-e6bf9015e6c5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# alpha\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', alpha=.5) + ggtitle(\"Without measure\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', alpha=.5) + ggtitle(\"With measure\"),\n", "])" ] }, { "cell_type": "code", "execution_count": 9, "id": "b5ace38c-176e-4dca-afea-ee4d37de097d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# linetype\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', size=1, linetype='dashed') + ggtitle(\"Without measure\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', size=1, linetype='dashed') + ggtitle(\"With measure\"),\n", "])" ] }, { "cell_type": "code", "execution_count": 10, "id": "3759af3d-cbe9-4a75-9448-b7cc2aab89c7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# width\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', width=.4) + ggtitle(\"With measure\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', width=.4) + ggtitle(\"Without measure\"),\n", "])" ] }, { "cell_type": "markdown", "id": "d5a0a0d7-e166-4f4b-a785-5fe4967beb0f", "metadata": {}, "source": [ "### Standard parameters" ] }, { "cell_type": "code", "execution_count": 11, "id": "315b1807-7504-4aca-8c30-caafcc10f3d5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# show_legend\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', show_legend=True) + ggtitle(\"Without measure\", \"Default calc_total\"),\n", " waterfall_plot(data1, 'x', 'y', show_legend=True, calc_total=False) + ggtitle(\"Without measure\", \"calc_total=False\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', show_legend=True) + ggtitle(\"With measure\", \"Default calc_total, without absolute\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', show_legend=True, calc_total=False) + ggtitle(\"With measure\", \"calc_total=False, without absolute\"),\n", " waterfall_plot(data3, 'x', 'y', measure='m', show_legend=True) + ggtitle(\"With measure\", \"Default calc_total, with absolute\"),\n", " waterfall_plot(data3, 'x', 'y', measure='m', show_legend=True, calc_total=False) + ggtitle(\"With measure\", \"calc_total=False, with absolute\"),\n", "], ncol=2)" ] }, { "cell_type": "code", "execution_count": 12, "id": "d454a5c1-0b86-4f76-af24-7bb0138b2a65", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# tooltips\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', relative_tooltips='none', absolute_tooltips='none') + \\\n", " ggtitle(\"Without measure\", \"relative_tooltips='none',\\nabsolute_tooltips='none'\"),\n", " waterfall_plot(data1, 'x', 'y', relative_tooltips='detailed', absolute_tooltips='detailed') + \\\n", " ggtitle(\"Without measure\", \"relative_tooltips='detailed',\\nabsolute_tooltips='detailed'\"),\n", " waterfall_plot(data1, 'x', 'y', relative_tooltips=layer_tooltips().line(\"@..dy..: from @..initial.. to @..value..\").disable_splitting(), \\\n", " absolute_tooltips=layer_tooltips().line(\"@..flow_type..: @..value..\").disable_splitting()) + \\\n", " ggtitle(\"Without measure\", \"Customized tooltips\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', relative_tooltips='none', absolute_tooltips='none') + \\\n", " ggtitle(\"With measure\", \"relative_tooltips='none',\\nabsolute_tooltips='none'\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', relative_tooltips='detailed', absolute_tooltips='detailed') + \\\n", " ggtitle(\"With measure\", \"relative_tooltips='detailed',\\nabsolute_tooltips='detailed'\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', relative_tooltips=layer_tooltips().line(\"@..dy..: from @..initial.. to @..value..\").disable_splitting(), \\\n", " absolute_tooltips=layer_tooltips().line(\"@..flow_type..: @..value..\").disable_splitting()) + \\\n", " ggtitle(\"With measure\", \"Customized tooltips\"),\n", "], ncol=3)" ] }, { "cell_type": "markdown", "id": "5f7dcd1c-2683-48dd-b837-5c77a211454b", "metadata": {}, "source": [ "### Waterfall-specific parameters" ] }, { "cell_type": "code", "execution_count": 13, "id": "5f3f0eaf-837c-48e9-b3ca-a9e3ef5d2c56", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# sorted_value\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', sorted_value=True) + ggtitle(\"Without measure\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', sorted_value=True) + ggtitle(\"With measure\"),\n", "])" ] }, { "cell_type": "code", "execution_count": 14, "id": "4efd06f0-ed57-4920-bf15-788555c6ca69", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# threshold\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', threshold=200) + ggtitle(\"Without measure\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', threshold=200) + ggtitle(\"With measure\"),\n", "])" ] }, { "cell_type": "code", "execution_count": 15, "id": "97c3799d-67aa-4c85-b562-1b2141f4c58d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# max_values\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', max_values=2) + ggtitle(\"Without measure\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', max_values=2) + ggtitle(\"With measure\"),\n", "])" ] }, { "cell_type": "code", "execution_count": 16, "id": "6967f33f-bc0b-44b6-8eea-f2659d235a34", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Use threshold to skip zeros\n", "data_with_zeros = dict(\n", " x=['a', 'b', 'c', 'd', 't', 'a', 'b', 't'],\n", " y=[1, -2, 3, 0, None, 0, 2, None],\n", " m=['relative', 'relative', 'relative', 'relative', 'total', 'relative', 'relative', 'total'],\n", ")\n", "\n", "gggrid([\n", " waterfall_plot(data_with_zeros, 'x', 'y', measure='m'),\n", " waterfall_plot(data_with_zeros, 'x', 'y', measure='m', threshold=0),\n", "])" ] }, { "cell_type": "code", "execution_count": 17, "id": "9d7ebb15-38ae-4d2b-946d-17f893239a6e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# calc_total\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y') + ggtitle(\"Without measure\", \"Default\"),\n", " waterfall_plot(data1, 'x', 'y', calc_total=False) + ggtitle(\"Without measure\", \"calc_total=False\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m') + ggtitle(\"With measure\", \"Default\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', calc_total=False) + ggtitle(\"With measure\", \"calc_total=False\"),\n", "], ncol=2)" ] }, { "cell_type": "code", "execution_count": 18, "id": "96d80733-8f76-4573-bf26-0ee53a17cad3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# total_title\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', total_title=\"Result\", show_legend=True, absolute_tooltips='detailed'),\n", " waterfall_plot(data2, 'x', 'y', measure='m', total_title=\"Result\", show_legend=True, absolute_tooltips='detailed'),\n", "])" ] }, { "cell_type": "markdown", "id": "dbf21bda-d4b9-40ba-a60c-ca48b1dcd96d", "metadata": {}, "source": [ "### Control additional geometries" ] }, { "cell_type": "code", "execution_count": 19, "id": "78ee7e3d-e3de-4411-ab06-c8f052dfa3ab", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# hline\n", "gggrid([\n", " waterfall_plot(data2, 'x', 'y', measure='m', hline=element_line()),\n", " waterfall_plot(data2, 'x', 'y', measure='m', hline=element_line(blank=True)),\n", " waterfall_plot(data2, 'x', 'y', measure='m', hline=element_blank()),\n", " waterfall_plot(data2, 'x', 'y', measure='m', hline='blank'),\n", "], ncol=2)" ] }, { "cell_type": "code", "execution_count": 20, "id": "851574de-f71e-4d18-9440-43fad28c3579", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# hline_ontop\n", "waterfall_plot(data2, 'x', 'y', measure='m', hline=element_line(), hline_ontop=False)" ] }, { "cell_type": "code", "execution_count": 21, "id": "fb978e16-8970-4912-9e42-48826bfe0116", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# hline color\n", "waterfall_plot(data2, 'x', 'y', measure='m', hline=element_line(color=\"magenta\"))" ] }, { "cell_type": "code", "execution_count": 22, "id": "e762863e-fe2e-4f0d-a601-ace2d1e951e2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# hline size\n", "waterfall_plot(data2, 'x', 'y', measure='m', hline=element_line(size=2))" ] }, { "cell_type": "code", "execution_count": 23, "id": "d52b6edf-73c8-4b19-afd5-317ab8854e7f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# hline linetype\n", "waterfall_plot(data2, 'x', 'y', measure='m', hline=element_line(linetype='solid'))" ] }, { "cell_type": "code", "execution_count": 24, "id": "e53879d0-e343-4229-a704-ced71127be89", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# connector\n", "gggrid([\n", " waterfall_plot(data2, 'x', 'y', measure='m', width=.5, connector=element_line()),\n", " waterfall_plot(data2, 'x', 'y', measure='m', width=.5, connector=element_line(blank=True)),\n", " waterfall_plot(data2, 'x', 'y', measure='m', width=.5, connector=element_blank()),\n", " waterfall_plot(data2, 'x', 'y', measure='m', width=.5, connector='blank'),\n", "], ncol=2)" ] }, { "cell_type": "code", "execution_count": 25, "id": "066f4ea3-5b5b-4f89-b34e-60ac8ff33302", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# connector color\n", "waterfall_plot(data2, 'x', 'y', measure='m', width=.5, connector=element_line(color=\"magenta\"))" ] }, { "cell_type": "code", "execution_count": 26, "id": "487a7d84-d647-4f10-996b-1d3fe4cd098a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# connector size\n", "waterfall_plot(data2, 'x', 'y', measure='m', width=.5, connector=element_line(size=2))" ] }, { "cell_type": "code", "execution_count": 27, "id": "6b4698b8-211c-4709-9593-0f0be28f8c9c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# connector linetype\n", "waterfall_plot(data2, 'x', 'y', measure='m', width=.5, connector=element_line(linetype='dotted'))" ] }, { "cell_type": "code", "execution_count": 28, "id": "58836d0f-b7f0-40c0-ae97-3ce29672470c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# label\n", "gggrid([\n", " waterfall_plot(data2, 'x', 'y', measure='m', label=element_text()),\n", " waterfall_plot(data2, 'x', 'y', measure='m', label=element_text(blank=True)),\n", " waterfall_plot(data2, 'x', 'y', measure='m', label=element_blank()),\n", " waterfall_plot(data2, 'x', 'y', measure='m', label='blank'),\n", "], ncol=2)" ] }, { "cell_type": "code", "execution_count": 29, "id": "6c322564-3d7e-41a8-a6d9-9cf331a1eb59", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# label color\n", "gggrid([\n", " waterfall_plot(data2, 'x', 'y', measure='m', label=element_text(color=\"yellow\")),\n", " waterfall_plot(data2, 'x', 'y', measure='m', fill=\"lightgray\", label=element_text(color='flow_type'))\n", "])" ] }, { "cell_type": "code", "execution_count": 30, "id": "13b89165-f88d-4619-be33-6d1e7278c48b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# label family\n", "waterfall_plot(data2, 'x', 'y', measure='m', label=element_text(family=\"Courier\"))" ] }, { "cell_type": "code", "execution_count": 31, "id": "0f84345b-af6f-4f60-9ba4-e2d7ebb9810d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# label face\n", "waterfall_plot(data2, 'x', 'y', measure='m', label=element_text(face='bold_italic'))" ] }, { "cell_type": "code", "execution_count": 32, "id": "912eb4e7-7b9b-4251-b20b-82cc138475ca", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# label size\n", "waterfall_plot(data2, 'x', 'y', measure='m', label=element_text(size=10))" ] }, { "cell_type": "code", "execution_count": 33, "id": "f8ef8a97-cc14-4316-9f96-a4ebc58064cd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# label angle\n", "waterfall_plot(data2, 'x', 'y', measure='m', label=element_text(angle=45))" ] }, { "cell_type": "code", "execution_count": 34, "id": "38f6dcbc-b448-4e6b-92ba-23abf38bc509", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# label hjust/vjust\n", "def get_waterfall_with_justified_labels(hjust, vjust):\n", " return waterfall_plot(data2, 'x', 'y', measure='m', label=element_text(hjust=hjust, vjust=vjust)) + \\\n", " ggtitle(\"Justified labels\", \"hjust={0}, vjust={1}\".format(hjust, vjust))\n", "\n", "gggrid([\n", " get_waterfall_with_justified_labels(0, 0), get_waterfall_with_justified_labels(0, 1),\n", " get_waterfall_with_justified_labels(1, 0), get_waterfall_with_justified_labels(1, 1),\n", "], ncol=2)" ] }, { "cell_type": "code", "execution_count": 35, "id": "60449da7-d537-4b38-b5a5-f764260be30b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# label_format\n", "waterfall_plot(data2, 'x', 'y', measure='m', label_format=\"({.1f})\")" ] }, { "cell_type": "code", "execution_count": 36, "id": "fb95b4d2-e569-46e2-ab42-9912d3806d15", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# group\n", "data_with_groups = {\n", " 'x': ['A', 'C', 'T1', 'A', 'B', 'C', 'T2'],\n", " 'y': [2, -1, None, 1, 3, -2, 0.0],\n", " 'm': ['absolute', 'relative', 'total', 'absolute', 'relative', 'relative', 'total'],\n", " 'g': ['a', 'a', 'a', 'b', 'b', 'b', 'b'],\n", "}\n", "\n", "waterfall_plot(data_with_groups, 'x', 'y', measure='m', group='g') + facet_wrap(facets='g', scales='free_x')" ] }, { "cell_type": "markdown", "id": "6929b319-34fd-4b2c-91d6-8c62c1bc50eb", "metadata": {}, "source": [ "## Other Customizations" ] }, { "cell_type": "code", "execution_count": 37, "id": "95278b59-a904-4638-883f-60f34fa9cbdd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# fill and color\n", "\n", "gggrid([\n", " waterfall_plot(data3, 'x', 'y', measure='m', show_legend=True, width=.7, size=1, color=\"#777777\", label=element_text(color=\"#777777\")) + \\\n", " scale_fill_manual({\"Increase\": \"white\", \"Decrease\": \"black\", \"Absolute\": \"green\", \"Total\": \"yellow\"}) + \\\n", " ggtitle(\"Custom scale_fill_manual()\"),\n", " waterfall_plot(data3, 'x', 'y', measure='m', show_legend=True, width=.7, fill=\"black\", label=element_text(color='flow_type')) + \\\n", " scale_color_manual({\"Increase\": \"green\", \"Decrease\": \"yellow\", \"Absolute\": \"red\", \"Total\": \"#bbbbff\"}) + \\\n", " ggtitle(\"Custom scale_color_manual()\"),\n", " waterfall_plot(data3, 'x', 'y', measure='m', show_legend=True, width=.7, color=\"#777777\", label=element_text(color=\"#777777\")) + \\\n", " scale_fill_manual({\"Increase\": \"green\", \"Decrease\": \"red\", \"Absolute\": \"cyan\", \"Total\": \"yellow\"}, labels=[\"Up\", \"Down\", \"From zero\", \"Result\"]) + \\\n", " ggtitle(\"Custom flow type names\"),\n", "], ncol=1) + ggsize(1000, 800)" ] }, { "cell_type": "code", "execution_count": 38, "id": "85532cdd-73c8-40e2-922e-f3370e043ceb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# flip coordinates\n", "waterfall_plot(data2, 'x', 'y', measure='m') + coord_flip()" ] }, { "cell_type": "code", "execution_count": 39, "id": "1c9916f8-c82d-459a-bb35-609bcf38448b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# custom theme\n", "waterfall_plot(data2, 'x', 'y', measure='m') + theme_bw() + flavor_darcula()" ] }, { "cell_type": "markdown", "id": "5a9be86e-68ac-417d-b08d-f551d8deb98f", "metadata": {}, "source": [ "## Tests" ] }, { "cell_type": "markdown", "id": "3dfb594b-a818-4d89-b107-e76b96c781dd", "metadata": {}, "source": [ "### Boundary Value Analysis" ] }, { "cell_type": "code", "execution_count": 40, "id": "5e9ae148-d20f-4d7a-b1fc-a678ba806885", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "class BVATest:\n", " def __init__(self, data, title, show=True):\n", " self.data = data\n", " self.title = title\n", " self.show = show\n", "\n", " def to_plot(self):\n", " return gggrid([\n", " waterfall_plot(self.data, 'x', 'y', measure='m', calc_total=True) + \\\n", " ggtitle(self.title, \"calc_total=True\"),\n", " waterfall_plot(self.data, 'x', 'y', measure='m', calc_total=False) + \\\n", " ggtitle(self.title, \"calc_total=False\"),\n", " ])\n", "\n", "show_all = False\n", "bva_tests = [\n", " BVATest(\n", " data=dict(\n", " x=[],\n", " y=[],\n", " m=[],\n", " ),\n", " title=\"Empty dataset\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " x=[\"A\", \"T\"],\n", " y=[1, None],\n", " m=['relative', 'total'],\n", " ),\n", " title=\"One value dataset\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " x=[\"A\", \"A\", \"T\"],\n", " y=[1, 2, None],\n", " m=['relative', 'relative', 'total'],\n", " ),\n", " title=\"Repeated categories\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " x=[\"A\", \"T\"],\n", " y=[0, None],\n", " m=['relative', 'total'],\n", " ),\n", " title=\"Zero values\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " x=[\"A\", \"T\"],\n", " y=[-1, None],\n", " m=['relative', 'total'],\n", " ),\n", " title=\"Negative values\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " x=[1, 0],\n", " y=[1, None],\n", " m=['relative', 'total'],\n", " ),\n", " title=\"Numeric x\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " x=[\"A\", \"B\", \"T\"],\n", " y=[1, float('inf'), float('-inf')],\n", " m=['relative', 'relative', 'total'],\n", " ),\n", " title=\"Inf values\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " x=[\"A\", \"B\", \"C\", None],\n", " y=[1, 1, None, 1],\n", " m=['relative', None, 'relative', 'total'],\n", " ),\n", " title=\"None values\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " x=[\"A\", \"B\", \"C\", np.nan],\n", " y=[1, 1, np.nan, 1],\n", " m=['relative', np.nan, 'relative', 'total'],\n", " ),\n", " title=\"np.nan values\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " x=[\"A\", \"B\", \"T\"],\n", " y=[1, -1, None],\n", " m=['relative', 'relative', 'total'],\n", " ),\n", " title=\"Total is zero\",\n", " ),\n", "]\n", "\n", "gggrid([\n", " t.to_plot()\n", " for t in filter(lambda t: show_all or t.show, bva_tests)\n", "], ncol=1)" ] }, { "cell_type": "markdown", "id": "431578e4-ba1a-412c-8187-900593e94453", "metadata": {}, "source": [ "### Regression Testing" ] }, { "cell_type": "code", "execution_count": 41, "id": "729811ab-68a3-417f-b9a0-0c4e6811b17b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Total should be equal to [3, 4] (in labels and tooltips)\n", "tdata = dict(\n", " x=[\"A\", \"B\", \"T1\", \"A\", \"B\", \"T2\"],\n", " y=[1, 2, None, -1, 2, 0],\n", " m=['relative', 'relative', 'total'] * 2\n", ")\n", "waterfall_plot(tdata, 'x', 'y', measure='m')" ] }, { "cell_type": "code", "execution_count": 42, "id": "dc6cd1f0-29b5-4d65-b086-ad78509485d7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Change of hline properties shouldn't affect to the further plots. The same is for connector and label.\n", "gggrid([\n", " waterfall_plot(data2, 'x', 'y', measure='m', hline=element_line(color=\"magenta\")),\n", " waterfall_plot(data2, 'x', 'y', measure='m', hline=element_line()),\n", " waterfall_plot(data2, 'x', 'y', measure='m', connector=element_line(color=\"magenta\")),\n", " waterfall_plot(data2, 'x', 'y', measure='m', connector=element_line()),\n", " waterfall_plot(data2, 'x', 'y', measure='m', label=element_text(color=\"black\")),\n", " waterfall_plot(data2, 'x', 'y', measure='m', label=element_text()),\n", "], ncol=2)" ] }, { "cell_type": "code", "execution_count": 43, "id": "47aa7e2a-43a2-43d9-a420-b32bf8b1d9de", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Change of total_title shouldn't affect to the further plots\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', total_title=\"Result\"),\n", " waterfall_plot(data1, 'x', 'y'),\n", "])" ] }, { "cell_type": "code", "execution_count": 44, "id": "fe77face-0bb7-4ca3-a0a4-503b525bf67c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# xxx_tooltips='none' should disable tooltips\n", "gggrid([\n", " waterfall_plot(data1, 'x', 'y', relative_tooltips='none', absolute_tooltips='none'),\n", " waterfall_plot(data2, 'x', 'y', measure='m', relative_tooltips='none', absolute_tooltips='none'),\n", "])" ] }, { "cell_type": "markdown", "id": "1404490f-087a-4984-ba77-a1da6a807c4e", "metadata": {}, "source": [ "## New Features" ] }, { "cell_type": "markdown", "id": "bc717f44-e0e7-49e5-8bd2-0c58e52f23fd", "metadata": {}, "source": [ "### Param `base`" ] }, { "cell_type": "code", "execution_count": 45, "id": "77a21548-ccda-412b-a491-484bf5e9b0c4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " waterfall_plot(data1, 'x', 'y') + ggtitle(\"Without measure\", \"Default\"),\n", " waterfall_plot(data1, 'x', 'y', base=-100) + ggtitle(\"Without measure\", \"base=-100\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m') + ggtitle(\"With measure\", \"Default\"),\n", " waterfall_plot(data2, 'x', 'y', measure='m', base=-100) + ggtitle(\"With measure\", \"base=-100\"),\n", "], ncol=2)" ] } ], "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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }