In [1]:
from pymatgen.matproj.rest import MPRester
from pymatgen.phasediagram.pdmaker import PhaseDiagram

%matplotlib inline

Generating the phase diagram

To generate a phase diagram, we obtain entries from the Materials Project and call the PhaseDiagram class in pymatgen.

In [2]:
#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)

Plotting the phase diagram

To plot a phase diagram, we send our phase diagram object into the PDPlotter class.

In [3]:
from pymatgen.phasediagram.plotter import PDPlotter
#Let's show all phases, including unstable ones
plotter = PDPlotter(pd, show_unstable=True)
plotter.show()

Calculating energy above hull and other phase equilibria properties

To perform more sophisticated analyses, use the PDAnalyzer object.

In [4]:
from pymatgen.phasediagram.pdanalyzer import PDAnalyzer
a = PDAnalyzer(pd)
In [5]:
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)
   Materials ID Composition     Ehull                           Decomposition
0         mp-21          Ca  0.013583                                1.00 Ca2
1      mp-10683          Ca  0.396161                                1.00 Ca2
2        mp-166          Ca  0.013330                                1.00 Ca2
3        mp-132          Ca  0.000000                                1.00 Ca2
4         mp-45          Ca  0.000508                                1.00 Ca2
5        mp-169           C  0.004413                                1.00 C16
6     mp-569416           C  0.028801                                1.00 C16
7         mp-66           C  0.135631                                1.00 C16
8         mp-47           C  0.160479                                1.00 C16
9         mp-48           C  0.002697                                1.00 C16
10    mp-611426           C  0.145308                                1.00 C16
11    mp-616440           C  0.140564                                1.00 C16
12    mp-611448           C  0.142026                                1.00 C16
13    mp-569567           C  0.174406                                1.00 C16
14    mp-569517           C  0.145186                                1.00 C16
15        mp-24           C  0.832340                                1.00 C16
16    mp-569304           C  0.029940                                1.00 C16
17    mp-579909           C  0.944071                                1.00 C16
18    mp-624889           C  1.255021                                1.00 C16
19    mp-568410           C  0.506378                                1.00 C16
20    mp-570002           C  0.762539                                1.00 C16
21    mp-630227           C  0.393490                                1.00 C16
22    mp-667273           C  0.388760                                1.00 C16
23    mp-680372           C  0.407085                                1.00 C16
24    mp-683919           C  0.348592                                1.00 C16
25    mp-937760           C  0.003670                                1.00 C16
26    mp-568806           C  0.006175                                1.00 C16
27    mp-568286           C  0.000000                                1.00 C16
28    mp-568028           C  0.528945                                1.00 C16
29    mp-606949           C  0.003575                                1.00 C16
..          ...         ...       ...                                     ...
33    mp-611836          O2  0.013366                                 1.00 O8
34    mp-560602          O2  0.414823                                 1.00 O8
35    mp-607540          O2  0.112355                                 1.00 O8
36    mp-570697        CaC2  0.762125                     0.67 C16 + 0.33 Ca2
37      mp-2482        CaC2  0.022313                     0.67 C16 + 0.33 Ca2
38       mp-917        CaC2  0.014315                     0.67 C16 + 0.33 Ca2
39      mp-1575        CaC2  0.015081                     0.67 C16 + 0.33 Ca2
40    mp-684668        CaC2  0.068324                     0.67 C16 + 0.33 Ca2
41    mp-642822        CaC2  0.024576                     0.67 C16 + 0.33 Ca2
42    mp-545512         CaO  0.065085                             1.00 Ca1 O1
43      mp-2605         CaO  0.000000                             1.00 Ca1 O1
44    mp-634859        CaO2  0.007227                   0.67 Ca1 O1 + 0.33 O8
45     mp-20066         CO2  0.000000                              1.00 C4 O8
46    mp-556660          CO  0.692016                   0.25 C16 + 0.75 C4 O8
47     mp-11875          CO  0.668938                   0.25 C16 + 0.75 C4 O8
48    mp-561848        C3O2  0.513353                   0.40 C16 + 0.60 C4 O8
49    mp-556034         CO2  0.028775                              1.00 C4 O8
50     mp-11725         CO2  0.025395                              1.00 C4 O8
51    mp-644607         CO2  0.028281                              1.00 C4 O8
52      mp-3205       CaCO3  0.169799                          1.00 Ca2 C2 O6
53      mp-4626       CaCO3  0.024281                          1.00 Ca2 C2 O6
54    mp-558543    Ca(CO2)2  0.046302  0.07 C16 + 0.71 Ca2 C2 O6 + 0.21 C4 O8
55      mp-3953       CaCO3  0.000000                          1.00 Ca2 C2 O6
56    mp-561412       CaCO3  0.007359                          1.00 Ca2 C2 O6
57    mp-560265       CaCO3  0.008073                          1.00 Ca2 C2 O6
58    mp-556235       CaCO3  0.002015                          1.00 Ca2 C2 O6
59    mp-553939       CaCO3  0.013584                          1.00 Ca2 C2 O6
60    mp-641635       CaCO3  1.368847                          1.00 Ca2 C2 O6
61    mp-548403       CaCO3  0.312190                          1.00 Ca2 C2 O6
62    mp-696740       CaCO3  2.017768                          1.00 Ca2 C2 O6

[63 rows x 4 columns]
In [ ]: