{ "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!\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Version Check\n", "Note: Sunburst Charts are available in version 3.8+
\n", "Run `pip install plotly --upgrade` to update your Plotly version\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.10.0'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic Sunburst Plot ###" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "trace = go.Sunburst(\n", " labels=[\"Eve\", \"Cain\", \"Seth\", \"Enos\", \"Noam\", \"Abel\", \"Awan\", \"Enoch\", \"Azura\"],\n", " parents=[\"\", \"Eve\", \"Eve\", \"Seth\", \"Seth\", \"Eve\", \"Eve\", \"Awan\", \"Eve\" ],\n", " values=[10, 14, 12, 10, 2, 6, 6, 4, 4],\n", " outsidetextfont = {\"size\": 20, \"color\": \"#377eb8\"},\n", " marker = {\"line\": {\"width\": 2}},\n", ")\n", "\n", "layout = go.Layout(\n", " margin = go.layout.Margin(t=0, l=0, r=0, b=0)\n", ")\n", "\n", "py.iplot(go.Figure([trace], layout), filename='basic_sunburst_chart')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Branchvalues ###\n", "With branchvalues \"total\", the value of the parent represents the width of its wedge. In the example below, \"Enoch\" is 4 and \"Awan\" is 6 and so Enoch's width is 4/6ths of Awans. With branchvalues \"remainder\", the parent's width is determined by its own value plus those of its children. So, Enoch's width is 4/10ths of Awan's (4 / (6 + 4)).\n", "\n", "Note that this means that the sum of the values of the children cannot exceed the value of their parent when branchvalues \"total\". When branchvalues \"relative\" (the default), children will not take up all of the space below their parent (unless the parent is the root and it has a value of 0)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "trace = go.Sunburst(\n", " labels=[ \"Eve\", \"Cain\", \"Seth\", \"Enos\", \"Noam\", \"Abel\", \"Awan\", \"Enoch\", \"Azura\"],\n", " parents=[\"\", \"Eve\", \"Eve\", \"Seth\", \"Seth\", \"Eve\", \"Eve\", \"Awan\", \"Eve\" ],\n", " values=[ 65, 14, 12, 10, 2, 6, 6, 4, 4],\n", " branchvalues=\"total\",\n", " outsidetextfont = {\"size\": 20, \"color\": \"#377eb8\"},\n", " marker = {\"line\": {\"width\": 2}},\n", ")\n", "\n", "layout = go.Layout(\n", " margin = go.layout.Margin(t=0, l=0, r=0, b=0)\n", ")\n", "\n", "py.iplot(go.Figure([trace], layout), filename='basic_sunburst_chart_total_branchvalues')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sunburst with Repeated Labels" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "\n", "trace = go.Sunburst(\n", " ids=[\n", " \"North America\", \"Europe\", \"Australia\", \"North America - Football\", \"Soccer\",\n", " \"North America - Rugby\", \"Europe - Football\", \"Rugby\",\n", " \"Europe - American Football\",\"Australia - Football\", \"Association\",\n", " \"Australian Rules\", \"Autstralia - American Football\", \"Australia - Rugby\",\n", " \"Rugby League\", \"Rugby Union\"\n", " ],\n", " labels= [\n", " \"North
America\", \"Europe\", \"Australia\", \"Football\", \"Soccer\", \"Rugby\",\n", " \"Football\", \"Rugby\", \"American
Football\", \"Football\", \"Association\",\n", " \"Australian
Rules\", \"American
Football\", \"Rugby\", \"Rugby
League\",\n", " \"Rugby
Union\"\n", " ],\n", " parents=[\n", " \"\", \"\", \"\", \"North America\", \"North America\", \"North America\", \"Europe\",\n", " \"Europe\", \"Europe\",\"Australia\", \"Australia - Football\", \"Australia - Football\",\n", " \"Australia - Football\", \"Australia - Football\", \"Australia - Rugby\",\n", " \"Australia - Rugby\"\n", " ],\n", " outsidetextfont={\"size\": 20, \"color\": \"#377eb8\"},\n", " leaf={\"opacity\": 0.4},\n", " marker={\"line\": {\"width\": 2}}\n", ")\n", "\n", "layout = go.Layout(\n", " margin = go.layout.Margin(t=0, l=0, r=0, b=0),\n", " sunburstcolorway=[\"#636efa\",\"#ef553b\",\"#00cc96\"]\n", ")\n", "\n", "fig = go.Figure([trace], layout)\n", "\n", "py.iplot(fig, filename='repeated_labels_sunburst')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Large Number of Slices\n", "This example uses a [plotly grid attribute](https://plotly.com/python/reference/#layout-grid) for the suplots. Reference the row and column destination using the [domain](https://plotly.com/python/reference/#sunburst-domain) attribute." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "import pandas as pd\n", "\n", "df1 = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv')\n", "df2 = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/coffee-flavors.csv')\n", "\n", "trace1 = go.Sunburst(\n", " ids=df1.ids,\n", " labels=df1.labels,\n", " parents=df1.parents,\n", " domain=dict(column=0)\n", ")\n", "\n", "trace2 = go.Sunburst(\n", " ids=df2.ids,\n", " labels=df2.labels,\n", " parents=df2.parents,\n", " domain=dict(column=1),\n", " maxdepth=2\n", ")\n", "\n", "layout = go.Layout(\n", " grid=go.layout.Grid(columns=2, rows=1),\n", " margin = go.layout.Margin(t=0, l=0, r=0, b=0),\n", " sunburstcolorway=[\n", " \"#636efa\",\"#EF553B\",\"#00cc96\",\"#ab63fa\",\"#19d3f3\",\n", " \"#e763fa\", \"#FECB52\",\"#FFA15A\",\"#FF6692\",\"#B6E880\"\n", " ],\n", " extendsunburstcolors=True\n", ")\n", "\n", "data = [trace1, trace2]\n", "\n", "fig = go.Figure(data, layout)\n", "\n", "py.iplot(fig, filename='large_number_of_slices')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/reference/#sunburst for more information and chart attribute options!" ] }, { "cell_type": "code", "execution_count": 5, "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-req-build-nl381ouk\n", "Building wheels for collected packages: publisher\n", " Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /tmp/pip-ephem-wheel-cache-wbt7a39o/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n", "Successfully built publisher\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.13\n", " Uninstalling publisher-0.13:\n", " Successfully uninstalled publisher-0.13\n", "Successfully installed publisher-0.13\n", "\u001b[33mYou are using pip version 10.0.1, however version 19.0.3 is available.\n", "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\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", " 'sunburst-charts.ipynb', 'python/sunburst-charts/', 'Sunburst Charts',\n", " 'How to make Sunburst Charts.',\n", " title= 'Sunburst in Python | plotly',\n", " has_thumbnail='true', thumbnail='thumbnail/sunburst.gif', \n", " language='python', \n", " display_as='basic', order=6.1,\n", " ipynb='~notebook_demo/274/')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "notebook_metadata_filter": "all,-language_info,-kernelspec" }, "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }