{
"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",
"\n",
"#### Version Check\n",
"Plotly's python API is updated frequently. To upgrade, run `pip install plotly --upgrade`."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"#### Simple Example with Plotly and [Squarify](https://pypi.python.org/pypi/squarify)\n",
"Define the coordinate system for the returned rectangles: these values will range from x to x + width and y to y + height.\n",
"Then define your treemap values. The sum of the treemap values must equal the total area to be laid out (i.e. width `*` height). The values must be sorted in descending order and must be positive."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"import squarify\n",
"\n",
"x = 0.\n",
"y = 0.\n",
"width = 100.\n",
"height = 100.\n",
"\n",
"values = [500, 433, 78, 25, 25, 7]\n",
"\n",
"normed = squarify.normalize_sizes(values, width, height)\n",
"rects = squarify.squarify(normed, x, y, width, height)\n",
"\n",
"# Choose colors from http://colorbrewer2.org/ under \"Export\"\n",
"color_brewer = ['rgb(166,206,227)','rgb(31,120,180)','rgb(178,223,138)',\n",
" 'rgb(51,160,44)','rgb(251,154,153)','rgb(227,26,28)']\n",
"shapes = []\n",
"annotations = []\n",
"counter = 0\n",
"\n",
"for r in rects:\n",
" shapes.append( \n",
" dict(\n",
" type = 'rect', \n",
" x0 = r['x'], \n",
" y0 = r['y'], \n",
" x1 = r['x']+r['dx'], \n",
" y1 = r['y']+r['dy'],\n",
" line = dict( width = 2 ),\n",
" fillcolor = color_brewer[counter]\n",
" ) \n",
" )\n",
" annotations.append(\n",
" dict(\n",
" x = r['x']+(r['dx']/2),\n",
" y = r['y']+(r['dy']/2),\n",
" text = values[counter],\n",
" showarrow = False\n",
" )\n",
" )\n",
" counter = counter + 1\n",
" if counter >= len(color_brewer):\n",
" counter = 0\n",
"\n",
"# For hover text\n",
"trace0 = go.Scatter(\n",
" x = [ r['x']+(r['dx']/2) for r in rects ], \n",
" y = [ r['y']+(r['dy']/2) for r in rects ],\n",
" text = [ str(v) for v in values ], \n",
" mode = 'text',\n",
")\n",
" \n",
"layout = dict(\n",
" height=700, \n",
" width=700,\n",
" xaxis=dict(showgrid=False,zeroline=False),\n",
" yaxis=dict(showgrid=False,zeroline=False),\n",
" shapes=shapes,\n",
" annotations=annotations,\n",
" hovermode='closest'\n",
")\n",
"\n",
"# With hovertext\n",
"figure = dict(data=[trace0], layout=layout)\n",
"\n",
"# Without hovertext\n",
"# figure = dict(data=[Scatter()], layout=layout)\n",
"\n",
"py.iplot(figure, filename='squarify-treemap')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"See https://plotly.com/python/reference/ for more information and chart attribute options or https://pypi.python.org/pypi/squarify for more information about squarify!"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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 /private/var/folders/k_/zf24qrfn2kg710j9pdrxzrz40000gn/T/pip-bZ7TjY-build\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.10\n",
" Uninstalling publisher-0.10:\n",
" Successfully uninstalled publisher-0.10\n",
" Running setup.py install for publisher ... \u001b[?25ldone\n",
"\u001b[?25hSuccessfully installed publisher-0.10\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/chelsea/venv/venv2.7/lib/python2.7/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",
"/Users/chelsea/venv/venv2.7/lib/python2.7/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",
" 'treemap.ipynb', 'python/treemaps/', 'Python Treemaps | plotly',\n",
" 'How to make interactive treemap in Python with Plotly and Squarify. '\n",
" 'An examples of a treemap in Plotly using Squarify.',\n",
" title = 'Python Treemaps | plotly',\n",
" name = 'Treemaps',\n",
" thumbnail='thumbnail/treemap.jpg', language='python',\n",
" has_thumbnail='true', display_as='statistical', order=11,\n",
" ipynb= '~notebook_demo/29')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 1
}