from pymatgen.matproj.rest import MPRester
from pymatgen.phasediagram.pdmaker import PhaseDiagram
%matplotlib inline
To generate a phase diagram, we obtain entries from the Materials Project and call the PhaseDiagram class in pymatgen.
#This initializes the REST adaptor. You may need to put your own API key in as an arg.
a = MPRester()
#Entries are the basic unit for thermodynamic and other analyses in pymatgen.
#This gets all entries belonging to the Ca-C-O system.
entries = a.get_entries_in_chemsys(['Ca', 'C', 'O'])
#With entries, you can do many sophisticated analyses, like creating phase diagrams.
pd = PhaseDiagram(entries)
To plot a phase diagram, we send our phase diagram object into the PDPlotter class.
from pymatgen.phasediagram.plotter import PDPlotter
#Let's show all phases, including unstable ones
plotter = PDPlotter(pd, show_unstable=True)
plotter.show()
To perform more sophisticated analyses, use the PDAnalyzer object.
from pymatgen.phasediagram.pdanalyzer import PDAnalyzer
a = PDAnalyzer(pd)
import collections
data = collections.defaultdict(list)
for e in entries:
decomp, ehull = a.get_decomp_and_e_above_hull(e)
data["Materials ID"].append(e.entry_id)
data["Composition"].append(e.composition.reduced_formula)
data["Ehull"].append(ehull)
data["Decomposition"].append(" + ".join(["%.2f %s" % (v, k.composition.formula) for k, v in decomp.items()]))
from pandas import DataFrame
df = DataFrame(data, columns=["Materials ID", "Composition", "Ehull", "Decomposition"])
print(df)