You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: How to make Ternary Contour Plots in Python with Plotly.
26
-
display_as: scientific
25
+
description: How to make ternary contour plots with plotly
26
+
display_as: 3d_charts
27
27
has_thumbnail: true
28
-
ipynb: ~notebook_demo/40
29
28
language: python
30
29
layout: user-guide
31
-
name: Ternary Contour Plots
32
-
order: 10
30
+
name: Surface Triangulation
33
31
page_type: u-guide
34
-
permalink: python/ternary-contour/
35
-
thumbnail: thumbnail/ternary-contour.jpg
32
+
permalink: python/ternary-contours/
33
+
thumbnail: thumbnail/trisurf.jpg
34
+
title: Python Ternary contours | plotly
36
35
---
37
36
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.
# 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.
83
106
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))
0 commit comments