Skip to content

Instantly share code, notes, and snippets.

@bocklund
Last active January 28, 2026 03:26
Show Gist options
  • Select an option

  • Save bocklund/704f7e18e3bb70ec78f595a24a4a2fe9 to your computer and use it in GitHub Desktop.

Select an option

Save bocklund/704f7e18e3bb70ec78f595a24a4a2fe9 to your computer and use it in GitHub Desktop.
Plot PyCalphad phase diagram with ESPEI dataplot containing a fixed set of phases.
# Plot PyCalphad phase diagram with ESPEI dataplot containing a fixed set of phases.
# The legend generator should have the superset of all modeled phases and all data phases
# (usually this is what causes the colors to mismatch - the phases in the model only come from the model and the dataplot phases come only from the data).
from pycalphad import Database, equilibrium, variables as v, ternplot
from pycalphad.plot.utils import phase_legend
from espei.datasets import load_datasets, recursive_glob
from espei.plot import dataplot
import matplotlib.pyplot as plt
dbf = Database('COST507.tdb')
comps = ['AL', 'MG', 'SI', 'VA']
phases = list(dbf.phases.keys())
datasets = load_datasets(recursive_glob("input-data"))
legend_phases = list(dbf.phases.keys()) # + data phases, if applicable
def fixed_legend_generator(phase_names):
# fixed phases and colors
def f(_):
return phase_legend(phase_names)
return f
legend_generator_func = fixed_legend_generator(legend_phases)
conditions = {v.T: 1273, v.P: 101325, v.X('MG'): (0, 1, 0.05), v.X("SI"): (0, 1, 0.05)}
ax = ternplot(dbf, comps, phases, conditions, legend_generator=legend_generator_func)
dataplot(comps, phases, conditions, datasets, legend_generator=legend_generator_func, ax=ax)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment