{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ternary Plots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Make ternary plots in Plotly and Python!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Learn about API authentication here: https://plotly.com/python/getting-started\n", "
Find your api_key here: https://plotly.com/settings/api" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'1.9.9'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Basic Tenary Plot with Markers" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from plotly.offline import init_notebook_mode, iplot, plot\n", "import plotly.graph_objs as go\n", "init_notebook_mode()\n", "\n", "rawData = [\n", " {'journalist':75,'developer':25,'designer':0,'label':'point 1'},\n", " {'journalist':70,'developer':10,'designer':20,'label':'point 2'},\n", " {'journalist':75,'developer':20,'designer':5,'label':'point 3'},\n", " {'journalist':5,'developer':60,'designer':35,'label':'point 4'},\n", " {'journalist':10,'developer':80,'designer':10,'label':'point 5'},\n", " {'journalist':10,'developer':90,'designer':0,'label':'point 6'},\n", " {'journalist':20,'developer':70,'designer':10,'label':'point 7'},\n", " {'journalist':10,'developer':20,'designer':70,'label':'point 8'},\n", " {'journalist':15,'developer':5,'designer':80,'label':'point 9'},\n", " {'journalist':10,'developer':10,'designer':80,'label':'point 10'},\n", " {'journalist':20,'developer':10,'designer':70,'label':'point 11'},\n", "];\n", "\n", "def makeAxis(title, tickangle): \n", " return {\n", " 'title': title,\n", " 'titlefont': { 'size': 20 },\n", " 'tickangle': tickangle,\n", " 'tickfont': { 'size': 15 },\n", " 'tickcolor': 'rgba(0,0,0,0)',\n", " 'ticklen': 5,\n", " 'showline': True,\n", " 'showgrid': True\n", " }\n", "\n", "data = [{ \n", " 'type': 'scatterternary',\n", " 'mode': 'markers',\n", " 'a': [i for i in map(lambda x: x['journalist'], rawData)],\n", " 'b': [i for i in map(lambda x: x['developer'], rawData)],\n", " 'c': [i for i in map(lambda x: x['designer'], rawData)],\n", " 'text': [i for i in map(lambda x: x['label'], rawData)],\n", " 'marker': {\n", " 'symbol': 100,\n", " 'color': '#DB7365',\n", " 'size': 14,\n", " 'line': { 'width': 2 }\n", " },\n", " }]\n", "\n", "layout = {\n", " 'ternary': {\n", " 'sum': 100,\n", " 'aaxis': makeAxis('Journalist', 0),\n", " 'baxis': makeAxis('
Developer', 45),\n", " 'caxis': makeAxis('
Designer', -45)\n", " },\n", " 'annotations': [{\n", " 'showarrow': False,\n", " 'text': 'Simple Ternary Plot with Markers',\n", " 'x': 0.5,\n", " 'y': 1.3,\n", " 'font': {'size': 15}\n", " }]\n", "}\n", "\n", "fig = {'data': data, 'layout': layout}\n", "iplot(fig, validate=False)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Soil Types Ternary Plot" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import json\n", "from plotly.offline import init_notebook_mode, iplot\n", "import plotly.graph_objs as go\n", "import urllib2 # note this will only work in 2.7 now, will upload six package later for compatibility\n", "\n", "init_notebook_mode()\n" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": true }, "outputs": [], "source": [ "url = 'https://gist.githubusercontent.com/davenquinn/988167471993bc2ece29/raw/f38d9cb3dd86e315e237fde5d65e185c39c931c2/data.json'\n", "req = urllib2.Request(url)\n", "opener = urllib2.build_opener()\n", "f = opener.open(req)\n", "json = json.loads(f.read())" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def create_data(json):\n", " \n", " return ([dict(name=k, type='scatterternary', mode='lines', \n", " a=map(lambda x: x['clay'],json[k]), \n", " b=map(lambda x: x['sand'],json[k]),\n", " c=map(lambda x: x['silt'],json[k]), \n", " line={'color': '#c00'}) for k in json])" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data = create_data(json)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def makeAxis(title): \n", " return {\n", " 'title': title,\n", " 'ticksuffix': '%',\n", " 'min': 0.01,\n", " 'linewidth': 2,\n", " 'ticks': 'outside',\n", " 'ticklen': 8,\n", " 'showgrid': True,\n", " }" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": true }, "outputs": [], "source": [ "layout = {\n", " 'ternary': {\n", " 'sum': 100,\n", " 'aaxis': makeAxis('Clay'),\n", " 'baxis': makeAxis('Sand'),\n", " 'caxis': makeAxis('Silt')\n", " },\n", " 'showlegend': False,\n", " 'width': 700,\n", " 'annotations': [{\n", " 'showarrow': False,\n", " 'text': 'Replica of Daven Quinn\\'s block',\n", " 'x': 0.50,\n", " 'y': 1.3\n", " }]\n", " }\n", "\n" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fig = {'data': data, 'layout': layout}" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "iplot(fig, validate=False)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "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": [ "Requirement already up-to-date: publisher in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages\r\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/2.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 publisher --upgrade\n", "# import publisher\n", "# publisher.publish(\n", "# 'ternary.ipynb', 'python/ternary-plots/', 'Python Ternary Plots | plotly',\n", "# 'How to make Ternary plots in Python with Plotly.',\n", "# name = 'Ternary',\n", "# thumbnail='thumbnail/ternary.jpg', language='python',\n", "# page_type='example_index', has_thumbnail='true', display_as='scientific', order=32) " ] }, { "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.11" } }, "nbformat": 4, "nbformat_minor": 0 }