{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n", "
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Version Check\n", "Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.8.1'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple Waterfall Chart" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.plotly.v1+json": { "config": { "linkText": "Export to plot.ly", "plotlyServerURL": "https://plotly.com", "responsive": true, "showLink": false }, "data": [ { "connector": { "line": { "color": "rgb(63, 63, 63)" } }, "measure": [ "relative", "relative", "total", "relative", "relative", "total" ], "name": "20", "orientation": "v", "text": [ "+60", "+80", "", "-40", "-20", "Total" ], "textposition": "outside", "type": "waterfall", "uid": "d468398e-b2c0-4d27-a6cc-1a85334ffaab", "x": [ "Sales", "Consulting", "Net revenue", "Purchases", "Other expenses", "Profit before tax" ], "y": [ 60, 80, 0, -40, -20, 0 ] } ], "layout": { "showlegend": true, "title": { "text": "Profit and loss statement 2018" } } }, "text/html": [ "
\n", " \n", " \n", "
\n", " \n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly.offline as py\n", "import plotly.graph_objs as go\n", "py.init_notebook_mode(connected = False)\n", "\n", "trace = go.Waterfall(\n", " name = \"20\", orientation = \"v\", \n", " measure = [\"relative\", \"relative\", \"total\", \"relative\", \"relative\", \"total\"], \n", " x = [\"Sales\", \"Consulting\", \"Net revenue\", \"Purchases\", \"Other expenses\", \"Profit before tax\"], \n", " textposition = \"outside\", \n", " text = [\"+60\", \"+80\", \"\", \"-40\", \"-20\", \"Total\"], \n", " y = [60, 80, 0, -40, -20, 0], \n", " connector = {\"line\":{\"color\":\"rgb(63, 63, 63)\"}}, \n", ")\n", "\n", "layout = go.Layout(\n", " title = \"Profit and loss statement 2018\", \n", " showlegend = True\n", ")\n", "\n", "py.iplot(go.Figure([trace], layout), filename = \"basic_waterfall_chart\")\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multi Category Waterfall Chart\n", "This example uses the [waterfallgroupgap attribute](https://plotly.com/python/reference/#layout-waterfallgroupgap), which sets a gap between bars. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.plotly.v1+json": { "config": { "linkText": "Export to plot.ly", "plotlyServerURL": "https://plotly.com", "responsive": true, "showLink": false }, "data": [ { "base": 1000, "measure": [ "absolute", "relative", "relative", "relative", "total", "relative", "relative", "relative", "total" ], "type": "waterfall", "uid": "dae8552f-abaa-465a-a3d4-4325015d1f02", "x": [ [ "2016", "2017", "2017", "2017", "2017", "2018", "2018", "2018", "2018" ], [ "initial", "q1", "q2", "q3", "total", "q1", "q2", "q3", "total" ] ], "y": [ 1, 2, 3, -1, null, 1, 2, -4, null ] }, { "base": 1000, "measure": [ "absolute", "relative", "relative", "relative", "total", "relative", "relative", "relative", "total" ], "type": "waterfall", "uid": "22c96764-c416-4bde-a1f3-6c9a4160e41a", "x": [ [ "2016", "2017", "2017", "2017", "2017", "2018", "2018", "2018", "2018" ], [ "initial", "q1", "q2", "q3", "total", "q1", "q2", "q3", "total" ] ], "y": [ 1.1, 2.2, 3.3, -1.1, null, 1.1, 2.2, -4.4, null ] } ], "layout": { "waterfallgroupgap": 0.5, "xaxis": { "tickfont": { "size": 16 }, "ticks": "outside", "title": { "text": "MULTI-CATEGORY" } } } }, "text/html": [ "
\n", " \n", " \n", "
\n", " \n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly.offline as py\n", "import plotly.graph_objs as go\n", "py.init_notebook_mode(connected = False)\n", "\n", "trace1 = go.Waterfall(\n", " x = [[\"2016\", \"2017\", \"2017\", \"2017\", \"2017\", \"2018\", \"2018\", \"2018\", \"2018\"],\n", " [\"initial\", \"q1\", \"q2\", \"q3\", \"total\", \"q1\", \"q2\", \"q3\", \"total\"]],\n", " measure = [\"absolute\", \"relative\", \"relative\", \"relative\", \"total\", \"relative\", \"relative\", \"relative\", \"total\"],\n", " y = [1, 2, 3, -1, None, 1, 2, -4, None],\n", " base = 1000\n", ")\n", "trace2 = go.Waterfall(\n", " x = [[\"2016\", \"2017\", \"2017\", \"2017\", \"2017\", \"2018\", \"2018\", \"2018\", \"2018\"],\n", " [\"initial\", \"q1\", \"q2\", \"q3\", \"total\", \"q1\", \"q2\", \"q3\", \"total\"]],\n", " measure = [\"absolute\", \"relative\", \"relative\", \"relative\", \"total\", \"relative\", \"relative\", \"relative\", \"total\"],\n", " y = [1.1, 2.2, 3.3, -1.1, None, 1.1, 2.2, -4.4, None],\n", " base = 1000\n", ")\n", "\n", "layout = go.Layout(\n", " waterfallgroupgap = 0.5,\n", " xaxis = {\"title\":\"MULTI-CATEGORY\",\"tickfont\":{\"size\":16},\"ticks\":\"outside\"}\n", ")\n", "\n", "data = [trace1,trace2]\n", "fig = go.Figure(data, layout)\n", "py.iplot(fig,filename = \"mutli_category_waterfall\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setting Marker Size and Color\n", "This example uses [decreasing, increasing, and total](https://plotly.com/python/reference/#waterfall-increasing) attributes to customize the bars." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.plotly.v1+json": { "config": { "linkText": "Export to plot.ly", "plotlyServerURL": "https://plotly.com", "responsive": true, "showLink": false }, "data": [ { "base": 300, "decreasing": { "marker": { "color": "Maroon", "line": { "color": "red", "width": 2 } } }, "increasing": { "marker": { "color": "Teal" } }, "measure": [ "absolute", "relative", "relative", "relative", "total", "relative", "relative", "relative", "total" ], "totals": { "marker": { "color": "deep sky blue", "line": { "color": "blue", "width": 3 } } }, "type": "waterfall", "uid": "c2e4965e-ad1f-4dd9-969a-5993a6c0cefe", "x": [ [ "2016", "2017", "2017", "2017", "2017", "2018", "2018", "2018", "2018" ], [ "initial", "q1", "q2", "q3", "total", "q1", "q2", "q3", "total" ] ], "y": [ 10, 20, 30, -10, null, 10, 20, -40, null ] } ], "layout": { "title": { "text": "Profit and loss statement" }, "waterfallgap": 0.3, "xaxis": { "tickfont": { "size": 15 }, "ticks": "outside", "title": { "text": "" } } } }, "text/html": [ "
\n", " \n", " \n", "
\n", " \n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly.offline as py\n", "import plotly.graph_objs as go\n", "py.init_notebook_mode(connected = False)\n", "\n", "trace = go.Waterfall(\n", " x = [[\"2016\", \"2017\", \"2017\", \"2017\", \"2017\", \"2018\", \"2018\", \"2018\", \"2018\"], \n", " [\"initial\", \"q1\", \"q2\", \"q3\", \"total\", \"q1\", \"q2\", \"q3\", \"total\"]], \n", " measure = [\"absolute\", \"relative\", \"relative\", \"relative\", \"total\", \"relative\", \"relative\", \"relative\", \"total\"], \n", " y = [10, 20, 30, -10, None, 10, 20, -40, None], base = 300, \n", " decreasing = {\"marker\":{\"color\":\"Maroon\", \"line\":{\"color\":\"red\", \"width\":2}}}, \n", " increasing = {\"marker\":{\"color\":\"Teal\"}}, \n", " totals = {\"marker\":{\"color\":\"deep sky blue\", \"line\":{\"color\":'blue', \"width\":3}}}\n", ")\n", "\n", "layout = go.Layout(\n", " title = \"Profit and loss statement\", \n", " waterfallgap = 0.3, \n", " xaxis = {\"title\":\"\", \"tickfont\":{\"size\":15}, \"ticks\":\"outside\"}\n", ")\n", "\n", "py.iplot(go.Figure([trace], layout), filename = \"Style_waterfall\")\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Horizontal Waterfall Chart" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.plotly.v1+json": { "config": { "linkText": "Export to plot.ly", "plotlyServerURL": "https://plotly.com", "responsive": true, "showLink": false }, "data": [ { "connector": { "line": { "color": "rgb(0, 0, 0)", "dash": "solid", "width": 4 }, "mode": "between" }, "measure": [ "relative", "relative", "relative", "relative", "total", "relative", "relative", "relative", "relative", "total", "relative", "relative", "total", "relative", "total" ], "name": "2018", "orientation": "h", "type": "waterfall", "uid": "4e169c0e-ee7d-4ecf-b96c-a96788cfad46", "x": [ 375, 128, 78, 27, null, -327, -12, -78, -12, null, 32, 89, null, -45, null ], "y": [ "Sales", "Consulting", "Maintenance", "Other revenue", "Net revenue", "Purchases", "Material expenses", "Personnel expenses", "Other expenses", "Operating profit", "Investment income", "Financial income", "Profit before tax", "Income tax (15%)", "Profit after tax" ] } ], "layout": { "margin": { "l": 150 }, "showlegend": true, "title": { "text": "Profit and loss statement 2018
waterfall chart displaying positive and negative" }, "xaxis": { "type": "linear" }, "yaxis": { "autorange": "reversed", "type": "category" } } }, "text/html": [ "
\n", " \n", " \n", "
\n", " \n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly.offline as py\n", "import plotly.graph_objs as go\n", "py.init_notebook_mode(connected = False)\n", "\n", "trace = go.Waterfall(\n", " name = \"2018\", orientation = \"h\", measure = [\"relative\", \"relative\", \"relative\", \"relative\", \"total\", \"relative\", \n", " \"relative\", \"relative\", \"relative\", \"total\", \"relative\", \"relative\", \"total\", \"relative\", \"total\"], \n", " y = [\"Sales\", \"Consulting\", \"Maintenance\", \"Other revenue\", \"Net revenue\", \"Purchases\", \"Material expenses\", \n", " \"Personnel expenses\", \"Other expenses\", \"Operating profit\", \"Investment income\", \"Financial income\", \n", " \"Profit before tax\", \"Income tax (15%)\", \"Profit after tax\"], \n", " x = [375, 128, 78, 27, None, -327, -12, -78, -12, None, 32, 89, None, -45, None], \n", " connector = {\"mode\":\"between\", \"line\":{\"width\":4, \"color\":\"rgb(0, 0, 0)\", \"dash\":\"solid\"}}\n", ")\n", "\n", "layout = go.Layout(\n", " title = \"Profit and loss statement 2018
waterfall chart displaying positive and negative\", \n", " yaxis = {\"type\":\"category\", \"autorange\":\"reversed\"}, \n", " xaxis = {\"type\":\"linear\"}, \n", " margin = {\"l\":150}, \n", " showlegend = True\n", ")\n", "\n", "py.iplot(go.Figure([trace], layout), filename = \"horizontal_waterfall\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/reference/#waterfall for more information and chart attribute options!" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting git+https://github.com/plotly/publisher.git\n", " Cloning https://github.com/plotly/publisher.git to /tmp/pip-1dh9l2dv-build\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.13\n", " Uninstalling publisher-0.13:\n", " Successfully uninstalled publisher-0.13\n", " Running setup.py install for publisher ... \u001b[?25ldone\n", "\u001b[?25hSuccessfully installed publisher-0.13\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/home/mahdis/.virtualenvs/plotly3.3/lib/python3.6/site-packages/IPython/nbconvert.py:13: ShimWarning:\n", "\n", "The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n", "\n", "/home/mahdis/.virtualenvs/plotly3.3/lib/python3.6/site-packages/publisher/publisher.py:53: UserWarning:\n", "\n", "Did you \"Save\" this notebook before running this command? Remember to save, always save.\n", "\n" ] } ], "source": [ "from IPython.display import display, HTML\n", "\n", "display(HTML(''))\n", "display(HTML(''))\n", "\n", "! pip install git+https://github.com/plotly/publisher.git --upgrade\n", "import publisher\n", "publisher.publish(\n", " 'waterfall-charts.ipynb', 'python/waterfall-charts/', 'Waterfall Charts',\n", " 'How to make waterfall plots in Python with Plotly.',\n", " title= 'Python Waterfall Chart | Plotly',\n", " has_thumbnail= 'true', thumbnail= 'thumbnail/waterfall-charts.jpg',\n", " language= 'python',\n", " # page_type= 'example_index', // note this is only if you want the tutorial to appear on the main page: plot.ly/python\n", " display_as= 'basic', order= 6.2, ipynb= '/~notebook_demo/276',uses_plotly_offline= True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }