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