Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9efb356

Browse files
authoredSep 4, 2019
Merge pull request #107 from plotly/ternary-contour
ternary contour notebook
2 parents 4c5808b + 168ca5a commit 9efb356

File tree

2 files changed

+108
-43
lines changed

2 files changed

+108
-43
lines changed
 

‎python/ternary-contour.md

Lines changed: 107 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,63 +22,127 @@ jupyter:
2222
pygments_lexer: ipython3
2323
version: 3.6.7
2424
plotly:
25-
description: How to make Ternary Contour Plots in Python with Plotly.
25+
description: How to make Ternary Contour Plots in Python with plotly
2626
display_as: scientific
2727
has_thumbnail: true
28-
ipynb: ~notebook_demo/40
2928
language: python
3029
layout: user-guide
31-
name: Ternary Contour Plots
32-
order: 10
30+
name: Ternary contours
3331
page_type: u-guide
3432
permalink: python/ternary-contour/
3533
thumbnail: thumbnail/ternary-contour.jpg
34+
title: Python Ternary contours | plotly
3635
---
3736

38-
#### Basic Ternary Contour Plot
37+
## Ternary contour plots
38+
39+
40+
A ternary contour plots represents isovalue lines of a quantity defined inside a [ternary diagram](https://en.wikipedia.org/wiki/Ternary_plot), i.e. as a function of three variables which sum is constant. Coordinates of the ternary plot often correspond to concentrations of three species, and the quantity represented as contours is some property (e.g., physical, chemical, thermodynamical) varying with the composition.
41+
42+
For ternary contour plots, use the figure factory ``create_ternary_contour``. The figure factory interpolates between given data points in order to compute the contours.
43+
44+
Below we represent an example from metallurgy, where the mixing enthalpy is represented as a contour plot for aluminum-copper-yttrium (Al-Cu-Y) alloys.
45+
46+
#### Simple ternary contour plot with plotly
3947

4048
```python
41-
import plotly.graph_objects as go
42-
43-
import requests
44-
45-
url = 'https://gist.githubusercontent.com/davenquinn/988167471993bc2ece29/raw/f38d9cb3dd86e315e237fde5d65e185c39c931c2/data.json'
46-
data = requests.get(url).json()
47-
48-
colors = ['#8dd3c7','#ffffb3','#bebada',
49-
'#fb8072','#80b1d3','#fdb462',
50-
'#b3de69','#fccde5','#d9d9d9',
51-
'#bc80bd','#ccebc5','#ffed6f'];
52-
53-
# generate a,b and c from JSON data..
54-
fig = go.Figure()
55-
56-
color_iter = iter(colors)
57-
for i in data.keys():
58-
fig.add_trace(go.Scatterternary(
59-
text=i,
60-
a=[ k['clay'] for k in data[i] ],
61-
b=[ k['sand'] for k in data[i] ],
62-
c=[ k['silt'] for k in data[i] ],
63-
mode='lines',
64-
line=dict(color='#444'),
65-
fill='toself',
66-
fillcolor=color_iter.__next__()
67-
))
68-
69-
fig.update_layout(
70-
title = 'Simple Ternary Contour Plot with Python',
71-
showlegend = False,
72-
ternary = {
73-
'sum':100,
74-
'aaxis':{'title': 'clay', 'ticksuffix':'%', 'min': 0.01, 'linewidth':2, 'ticks':'outside' },
75-
'baxis':{'title': 'sand', 'ticksuffix':'%', 'min': 0.01, 'linewidth':2, 'ticks':'outside' },
76-
'caxis':{'title': 'silt', 'ticksuffix':'%', 'min': 0.01, 'linewidth':2, 'ticks':'outside' }},
77-
)
49+
import plotly.figure_factory as ff
50+
import numpy as np
51+
Al = np.array([0. , 0. , 0., 0., 1./3, 1./3, 1./3, 2./3, 2./3, 1.])
52+
Cu = np.array([0., 1./3, 2./3, 1., 0., 1./3, 2./3, 0., 1./3, 0.])
53+
Y = 1 - Al - Cu
54+
# synthetic data for mixing enthalpy
55+
# See https://pycalphad.org/docs/latest/examples/TernaryExamples.html
56+
enthalpy = (Al - 0.01) * Cu * (Al - 0.52) * (Cu - 0.48) * (Y - 1)**2
57+
fig = ff.create_ternary_contour(np.array([Al, Y, Cu]), enthalpy,
58+
pole_labels=['Al', 'Y', 'Cu'],
59+
interp_mode='cartesian')
60+
fig.show()
61+
```
7862

79-
fig.show()
63+
#### Customized ternary contour plot
64+
65+
```python
66+
import plotly.figure_factory as ff
67+
import numpy as np
68+
Al = np.array([0. , 0. , 0., 0., 1./3, 1./3, 1./3, 2./3, 2./3, 1.])
69+
Cu = np.array([0., 1./3, 2./3, 1., 0., 1./3, 2./3, 0., 1./3, 0.])
70+
Y = 1 - Al - Cu
71+
# synthetic data for mixing enthalpy
72+
# See https://pycalphad.org/docs/latest/examples/TernaryExamples.html
73+
enthalpy = 2.e6 * (Al - 0.01) * Cu * (Al - 0.52) * (Cu - 0.48) * (Y - 1)**2 - 5000
74+
fig = ff.create_ternary_contour(np.array([Al, Y, Cu]), enthalpy,
75+
pole_labels=['Al', 'Y', 'Cu'],
76+
interp_mode='cartesian',
77+
ncontours=20,
78+
colorscale='Viridis',
79+
showscale=True,
80+
title='Mixing enthalpy of ternary alloy')
81+
fig.show()
8082
```
8183

84+
#### Ternary contour plot with lines only
85+
8286
```python
87+
import plotly.figure_factory as ff
88+
import numpy as np
89+
Al = np.array([0. , 0. , 0., 0., 1./3, 1./3, 1./3, 2./3, 2./3, 1.])
90+
Cu = np.array([0., 1./3, 2./3, 1., 0., 1./3, 2./3, 0., 1./3, 0.])
91+
Y = 1 - Al - Cu
92+
# synthetic data for mixing enthalpy
93+
# See https://pycalphad.org/docs/latest/examples/TernaryExamples.html
94+
enthalpy = 2.e6 * (Al - 0.01) * Cu * (Al - 0.52) * (Cu - 0.48) * (Y - 1)**2 - 5000
95+
fig = ff.create_ternary_contour(np.array([Al, Y, Cu]), enthalpy,
96+
pole_labels=['Al', 'Y', 'Cu'],
97+
interp_mode='cartesian',
98+
ncontours=20,
99+
coloring='lines')
100+
fig.show()
101+
```
102+
103+
#### Ternary contour plot with data points
104+
105+
With `showmarkers=True`, data points used to compute the contours are also displayed. They are best visualized for contour lines (no solid coloring). At the moment data points lying on the edges of the diagram are not displayed, this will be improved in future versions.
83106

107+
```python
108+
import plotly.figure_factory as ff
109+
import numpy as np
110+
Al, Cu = np.mgrid[0:1:7j, 0:1:7j]
111+
Al, Cu = Al.ravel(), Cu.ravel()
112+
mask = Al + Cu <= 1
113+
Al, Cu = Al[mask], Cu[mask]
114+
Y = 1 - Al - Cu
115+
116+
enthalpy = (Al - 0.5) * (Cu - 0.5) * (Y - 1)**2
117+
fig = ff.create_ternary_contour(np.array([Al, Y, Cu]), enthalpy,
118+
pole_labels=['Al', 'Y', 'Cu'],
119+
ncontours=20,
120+
coloring='lines',
121+
showmarkers=True)
122+
fig.show()
123+
```
124+
125+
#### Interpolation mode
126+
127+
Two modes are available in order to interpolate between data points: interpolation in Cartesian space (`interp_mode='cartesian'`) or interpolation using the [isometric log-ratio transformation](https://link.springer.com/article/10.1023/A:1023818214614) (see also [preprint](https://www.researchgate.net/profile/Leon_Parent2/post/What_is_the_best_approach_for_diagnosing_nutrient_disorders_and_formulating_fertilizer_recommendations/attachment/59d62a69c49f478072e9cf3f/AS%3A272541220835360%401441990298625/download/Egozcue+et+al+2003.pdf)), `interp_mode='ilr'`. The `ilr` transformation preserves metrics in the [simplex](https://en.wikipedia.org/wiki/Simplex) but is not defined on its edges.
128+
129+
```python
130+
a, b = np.mgrid[0:1:20j, 0:1:20j]
131+
mask = a + b <= 1
132+
a, b = a[mask], b[mask]
133+
coords = np.stack((a, b, 1 - a - b))
134+
value = np.sin(3.2 * np.pi * (a + b)) + np.sin(3 * np.pi * (a - b))
135+
fig = ff.create_ternary_contour(coords, value, ncontours=9)
136+
fig.show()
137+
```
138+
139+
```python
140+
a, b = np.mgrid[0:1:20j, 0:1:20j]
141+
mask = a + b <= 1
142+
a, b = a[mask], b[mask]
143+
coords = np.stack((a, b, 1 - a - b))
144+
value = np.sin(3.2 * np.pi * (a + b)) + np.sin(3 * np.pi * (a - b))
145+
fig = ff.create_ternary_contour(coords, value, interp_mode='cartesian',
146+
ncontours=9)
147+
fig.show()
84148
```

‎requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ psutil
1616
requests
1717
networkx
1818
squarify
19+
scikit-image

0 commit comments

Comments
 (0)
Please sign in to comment.