# How to use Ternary Contour Plots

I have datas in a triangle, and I want to use Ternary Contour plot. But I do not know how to do it.

The examples do now show the information on the datas. Could somebody explain it more, or give an example?

Thank you very much!

Hi @leiniu,

The basic idea in https://plot.ly/python/ternary-contour/ is that for each region is represented by a ternary trace with `fill='toself'`. The trace specifies the outline of the region on the plot, and the `fill` option causes the region to be filled in with the trace’s line color.

You can take a look at the raw data for the example at https://gist.githubusercontent.com/davenquinn/988167471993bc2ece29/raw/f38d9cb3dd86e315e237fde5d65e185c39c931c2/data.json

Hope that helps get you started!
-Jon

Is it possible to create ternary contour plot in plotly with python as shown in the following figure:

The original question appeared in
https://stackoverflow.com/questions/29512046/how-to-create-ternary-contour-plot-in-python

Thank you very much!

Hi @leiniu,

There’s not single trace the will produce this plot. I think it should be possible, but It would take a bit of work. The general approach I’d recommend:

• Compute the contours you want outside of plotly using scikit image (http://scikit-image.org/docs/dev/auto_examples/edges/plot_contours.html).
• Draw each contour as a separate `scatterternary` trace with `mode='lines'`.
• Set `scatterternary.fill` to `'toself'` to get the filled in regions effect.
• Choose a colorscale from the colorlover library, and interpolate it (https://github.com/jackparmer/colorlover#clinterp-scale-r-) to have a number of states that matches the number of contour lines chosen by scikit-image.
• Use these colors to set the `scatterternary.line.color` properties of each trace.

-Jon

Thanks again. I found that there is a method for plotly with matlab as shown in the following:

``````To make this plot, you'll need the the Ternplot package
from the MATLAB [File exchange](http://www.mathworks.com/matlabcentral/fileexchange/2299-alchemyst-ternplot) % add data % An + Bn + Cn = 1 % Z controls contour 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]; % create figure fig = figure; terncontour(A, B, C, Z) ternlabel('A', 'B', 'C') % Plotly pf = fig2plotly(fig, 'strip', false, 'filename', 'YOUR_FILENAME'); % Edit the contour fill and color in Plotly's online editor
``````

Is it possible for plotly with python is such a simple way?

Hi @leiniu,

I don’t think there’s a simple way to build this plot in python directly.

-Jon

Hi. There is a guide for Ternary Contour Plot with python as shown in the following figure

I think this is an interesting example. Do you know how to find the reference for this plot? Thank you very much again.

This the webpage for Plotly with Python:
https://plot.ly/python/scientific-charts/

Thanks a good question @leiniu, I don’t know how that example was created.

@chriddyp, @chelsea, or @etienne do you remember how this thumbnail image for the “Ternary Contour Plots” (See post above) was created?

-Jon

@leiniu, @jmmease,
Here is a workaround for ternary contour plot: https://plot.ly/~empet/15011

and two contour plots: https://plot.ly/~empet/15007, https://plot.ly/~empet/15009

1 Like

Hi, @empet. Many thanks for your interesting and inspiring example. I really appreciate them.

By the way, when I try to run the code in your example, I found that the sides of the triangle are jagged. So I plot an additional trace defined bellow:
trace0 = go.Scatter(
x=[0.5,0,1,0.5],
y=[np.sqrt(3)/2,0,0,np.sqrt(3)/2],
line=dict(width=5, color=‘white’),
)
Then I plot this trace and the contour trace together. The sides of the triangle seem to be smooth. Is there any other regular way to smooth the boundary of the triangle?

Thank you again!

Hi @jmmease. @empet provides a possible way to create Ternary Contour Plot with python above. Thank you for your help.

Hi @leiniu, The triangle sides are jagged because of the rectangular meshgrid. A larger number, N, of grid points in both directions, can smooth the sides. Your solution to plot the side lines fixes this issue.