Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a ternary contour plot with Python Plotly?

Tags:

python

plotly

I am trying to plot a function defined on a simplex. Apparently, you can do it very nicely using the Matlab API.

enter image description here

But I cannot figure out how to do this using the Python API. The reference has a section named Ternary Contour Plots in Python but it does not plot a numeric function, but a function mapping the simplex to a given discrete set.

I tried to emulate the Matlab code:

import plotly as py

A = [0, .2, .2, .2, 0, .6, .75, .9, 0, 1, .8, .3]
B = [1, .2, .4, .1, 0, .4, .05, 0, .8, 0, .05, .3]
C = [0, .6, .4, .7, 1, 0, .2, .1, .2, 0, .15, .4]
Z = [.1, .5, .1, .2, 1, .8, .4, 0, .1, .6, 1, .7]

trace = {
    "type": 'scatterternary',
    "carpet": 'scattercontour',
    "a": A,
    "b": B,
    "c": C,
    "z": Z
}

layout = {
    'title': 'Simple Ternary Contour Plot with Python'
}

figure = dict(data=[trace], layout=layout)
py.offline.plot(figure, validate=False)

But I get this plot instead:

enter image description here

like image 209
Martin Drozdik Avatar asked Sep 05 '25 03:09

Martin Drozdik


1 Answers

If someone is still interested, here is a Jupyter Notebook to see how to generate a Plotly ternary contour plot.

like image 61
xecafe Avatar answered Sep 07 '25 20:09

xecafe