Brought to you by Marco A. Lopez-Sanchez - Last update: 2022-10-19

What is a ternary plot?

A ternary plot is a triangular diagram that displays the proportion of three variables that sum to a constant, usually 1 or 100%. It is a common diagram in solid-earth but also in other physical sciences.

What is the purpose of this repository?

You want to use ternary diagrams in your Jupyter notebook (or Python script). Sadly you realise that matplolib does not have ternary plots by default. After some research on the internet, you realise that the alternatives are either to install other plotting libraries (e.g. Plotly) or third-party libraries that rely on matplolib (e.g. python-ternary or mpltern). Unfortunately, you don't feel like learning a new syntax for plotting (you are too comfortable with your matplolib buddy) or don't want to install a new Python library with all that this entails (dependencies, etc.) and that it seem overkill for your needs*. This is where the good news comes in. You can create a barebones ternary diagram with the minimum necessary elements in matplolib in few lines of code. In short, no installations, no new dependencies, your usual matplolib syntax, and no fuss.

*I have nothing against these libraries, quite the opposite, if you need more advanced features use them!

How to use it?

Simply copy and paste the functions into your Jupyter notebook cells and follow the usage examples.

Below we define three different Python functions that convert from ternary to Cartesian coordinates

Finally, the function that create the ternary plot using matplolib

Alternatively, you can download the python file containing the functions here and save it to the same folder where your notebook is located. Then simply run the script using %run mpl_ternary.py or import it as a module. More details in https://github.com/marcoalopez/ternary_plots/blob/main/example_two.ipynb

Examples

primitives

Plotting some points for reference

first example

 

 

Real life examples

To use ternary diagrams it is always the same two-step process:

  1. Normalise and convert your ternary coordinates to Cartesian ones. For this you will use the tri2cart() function.
  2. Then initialize your ternary diagram using the ternary() function and use your ususal matplolib commands (plot(), scatter(), etc.)

Important: Usage examples will be available soon

 

License

Creative Commons Licence
The notebooks are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License and codes under Mozilla Public License 2.0.


Copyright © 2022 Marco A. Lopez-Sanchez

Information presented on this website and the notebooks is provided without any express or implied warranty and may include technical inaccuracies or typing errors; the author reserve the right to modify or enhance the content of this website as well as the notebooks at any time without previous notice. This webpage and the notebooks are not liable for the content of external links.

Hosted on GitHub Pages