Show Sidebar Hide Sidebar

Ternary Contour Plots in Python

How to make Ternary Contour Plots in Python with Plotly.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Note: Ternary Plots are available in version 1.9.10+
Run pip install plotly --upgrade to update your Plotly version

Basic Ternary Contour Plot

In [8]:
import plotly.plotly as py

import json
import urllib

url = 'https://gist.githubusercontent.com/davenquinn/988167471993bc2ece29/raw/f38d9cb3dd86e315e237fde5d65e185c39c931c2/data.json'
response = urllib.urlopen(url).read()
data = json.loads(response)

colors = ['#8dd3c7','#ffffb3','#bebada',
          '#fb8072','#80b1d3','#fdb462',
          '#b3de69','#fccde5','#d9d9d9',
          '#bc80bd','#ccebc5','#ffed6f'];

# generate a,b and c from JSON data..
traces = []
color_iter = iter(colors)
for i in data.keys():
    trace = dict(text=i,
        type='scatterternary',
        a=[ k['clay'] for k in data[i] ],
        b=[ k['sand'] for k in data[i] ],
        c=[ k['silt'] for k in data[i] ],
        mode='lines',
        line=dict(color='#444'),
        fill='toself',
        fillcolor=color_iter.next()
    )
    traces.append(trace)
    
layout = {
    'title': 'Simple Ternary Contour Plot with Python',
    'ternary': 
        {'sum':100,
         'aaxis':{'title': 'clay', 'ticksuffix':'%', 'min': 0.01, 'linewidth':2, 'ticks':'outside' },
         'baxis':{'title': 'sand', 'ticksuffix':'%', 'min': 0.01, 'linewidth':2, 'ticks':'outside' },
         'caxis':{'title': 'silt','ticksuffix':'%', 'min': 0.01, 'linewidth':2, 'ticks':'outside' }},
    'showlegend': False          
}

figure = dict(data=traces, layout=layout)
py.iplot(figure, validate=False)
Out[8]:
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.