{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to 'discretize' plotly choropleths" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import plotly.plotly as py\n", "import plotly.tools as tls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Starting with:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tls.embed('https://plot.ly/~MattSundquist/19045/executions-by-us-state-since-1819/')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "MattSundquist19045 = py.get_figure('https://plot.ly/~MattSundquist/19045/executions-by-us-state-since-1819/', raw=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Suggestion 1: with a discrete colorscale\n", "\n", "example:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tls.embed('https://plot.ly/~chris/15362/net-energy-imports-as-a-percentage-of-energy-use/')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Discrete colorscales look like:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[0.0, u'rgb(5,48,97)'],\n", " [0.18265666689933938, u'rgb(5,48,97)'],\n", " [0.18265666689933938, u'rgb(33,102,172)'],\n", " [0.36531333379867875, u'rgb(33,102,172)'],\n", " [0.36531333379867875, u'rgb(67,147,195)'],\n", " [0.5479700006980182, u'rgb(67,147,195)'],\n", " [0.5479700006980182, u'rgb(146,197,222)'],\n", " [0.7306266675973575, u'rgb(146,197,222)'],\n", " [0.7306266675973575, u'rgb(209,229,240)'],\n", " [0.9132833344966969, u'rgb(209,229,240)'],\n", " [0.9132833344966969, u'rgb(247,247,247)'],\n", " [0.9277361120805807, u'rgb(247,247,247)'],\n", " [0.9277361120805807, u'rgb(253,219,199)'],\n", " [0.9421888896644646, u'rgb(253,219,199)'],\n", " [0.9421888896644646, u'rgb(244,165,130)'],\n", " [0.9566416672483484, u'rgb(244,165,130)'],\n", " [0.9566416672483484, u'rgb(214,96,77)'],\n", " [0.9710944448322323, u'rgb(214,96,77)'],\n", " [0.9710944448322323, u'rgb(178,24,43)'],\n", " [0.9855472224161161, u'rgb(178,24,43)'],\n", " [0.9855472224161161, u'rgb(103,0,31)'],\n", " [1.0, u'rgb(103,0,31)']]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chris15362 = py.get_figure('https://plot.ly/~chris/15362/net-energy-imports-as-a-percentage-of-energy-use/', raw=True)\n", "\n", "chris15362['data'][0]['colorscale']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a function" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def make_discrete_colorscale(vals, colors):\n", " colorscale = []\n", " \n", " zmin = vals[0]\n", " zmax = vals[-1]\n", " d = float(zmax-zmin)\n", " \n", " vals_normed = [(val-zmin)/d for val in vals]\n", " \n", " for i, val in enumerate(vals_normed[:-1]):\n", " colorscale.append([val, colors[i]])\n", " colorscale.append([val, colors[i+1]])\n", " \n", " colorscale.append([vals_normed[-1], colors[-1]])\n", " \n", " return colorscale" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and make a discrete colorscale:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[0.0, '#ffffcc'],\n", " [0.0, '#addd8e'],\n", " [0.006666666666666667, '#addd8e'],\n", " [0.006666666666666667, '#78c679'],\n", " [0.06666666666666667, '#78c679'],\n", " [0.06666666666666667, '#31a354'],\n", " [0.6666666666666666, '#31a354'],\n", " [0.6666666666666666, '#006837'],\n", " [1.0, '#006837']]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vals = [0, 10, 100, 1000, 1500]\n", "colors = ['#ffffcc', '#addd8e', '#78c679', '#31a354', '#006837']\n", "\n", "colorscale = make_discrete_colorscale(vals, colors)\n", "\n", "colorscale" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "update the figure object:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "trace = MattSundquist19045['data'][0]\n", "\n", "trace['autocolorscale'] = False\n", "trace['colorscale'] = colorscale\n", "trace['zmin'] = 0\n", "trace['zmax'] = 1500\n", "trace['colorbar'] = dict(\n", " ticks='outside',\n", " tickmode='array',\n", " tickvals=vals,\n", " ticktext=[''] + vals[1::],\n", " x=0.9,\n", " title='State executions'\n", ")\n", "\n", "# some style suggestions:\n", "# there's a bug at the michigan/ontario boundary that shows up\n", "# with yout set of style attributes\n", "\n", "trace['marker']['line']['color'] = 'black'\n", "MattSundquist19045['layout']['geo']['showlakes'] = False" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "py.iplot(MattSundquist19045, validate=False, filename='discrete-colorscale')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Suggestion 2: split data into several choropleth traces" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[(0, '#ffffcc'),\n", " (10, '#addd8e'),\n", " (100, '#78c679'),\n", " (1000, '#31a354'),\n", " (1500, '#006837')]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zip(vals, colors)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ "trace = MattSundquist19045['data'][0]\n", "N = len(vals)\n", "data = []\n", "\n", "def make_choropleth(i, val_range):\n", " color = colors[i]\n", " \n", " return dict(\n", " type='choropleth',\n", " locationmode='USA-states',\n", " locations=[],\n", " z=[],\n", " text=[],\n", " colorscale=[[0, color], [1, color]],\n", " zmin=val_range[0],\n", " zmax=val_range[1],\n", " colorbar=dict(\n", " x=0.9,\n", " y=i/float(N) + 0.2,\n", " len=1/float(N),\n", " tick0=val_range[0],\n", " dtick=val_range[1]-val_range[0]\n", " )\n", " )\n", "\n", "for i in range(N-1):\n", " val_range = [vals[i], vals[i+1]]\n", " \n", " trace_new = make_choropleth(i, val_range)\n", " \n", " for loc, z, text in zip(trace['locations'], trace['z'], trace['text']):\n", " if val_range[0] <= z < val_range[1]:\n", " trace_new['locations'].append(loc)\n", " trace_new['z'].append(z)\n", " trace_new['text'].append(text)\n", " \n", " data.append(trace_new)\n", " \n", "fig = dict(\n", " data=data,\n", " layout=MattSundquist19045['layout']\n", ")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "py.iplot(fig, validate=False, filename='discrete-colorscale2')" ] }, { "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.6" } }, "nbformat": 4, "nbformat_minor": 0 }