Ternary Plots

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from pyrolite.plot import pyroplot

np.random.seed(82)

Let’s first create some example data:

df = pd.DataFrame(data=np.exp(np.random.rand(100, 3)), columns=["SiO2", "MgO", "CaO"])
df.loc[:, ["SiO2", "MgO", "CaO"]].head()
SiO2 MgO CaO
0 1.316828 1.895023 1.866191
1 2.177206 1.613029 2.609435
2 2.711316 2.299090 1.226108
3 1.143450 1.849684 1.809385
4 1.905569 2.687793 1.398168


Now we can create a simple scatter plot:

ax = df.loc[:, ["SiO2", "MgO", "CaO"]].pyroplot.scatter(c="k")
plt.show()
ternary

If the data represent some continuous series, you could also plot them as lines:

ax = df.loc[:, ["SiO2", "MgO", "CaO"]].pyroplot.plot(color="k", alpha=0.5)
plt.show()
ternary

The plotting axis can be specified to use exisiting axes:

fig, ax = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(12, 5))

df.loc[:, ["SiO2", "MgO", "CaO"]].sample(20).pyroplot.scatter(ax=ax[0], c="k")
df.loc[:, ["SiO2", "MgO", "CaO"]].sample(20).pyroplot.scatter(ax=ax[1], c="g")

ax = fig.orderedaxes  # creating scatter plots reorders axes, this is the correct order
plt.tight_layout()
ternary

Total running time of the script: (0 minutes 14.120 seconds)