Skip to content

Using PyMatGen

roeeasher edited this page Jun 18, 2026 · 3 revisions

🧪 PyMatGen

The Python Materials Genomics (PyMatGen) toolkit is one of the most powerful Python libraries in computational materials science.

Developed as a core part of the Materials Project, PyMatGen helps you analyze, manipulate, and generate crystal structures, perform symmetry analysis, and more.

🔗 Official Website:https://pymatgen.org/
📹 YouTube Tutorials:Materials Virtual Lab


🛠️ Installing PyMatGen

To install PyMatGen on your Mac or on the POWER cluster, activate your mamba environment and run:

mamba install pymatgen

⏳ Note: This may take a few minutes. PyMatGen has several dependencies.


✅ Verifying Your Installation

Let's start with a quick check to see if PyMatGen works:

frompymatgen.symmetry.groupsimportSpaceGroupsymmetrynumber=10print(SpaceGroup.from_int_number(symmetrynumber).symbol)

This should print the Hermann–Mauguin symbol for space group #10.

Now, let’s extend that to print all 230 space group symbols:

frompymatgen.symmetry.groupsimportSpaceGroupforsg_numinrange(1, 231):
sg=SpaceGroup.from_int_number(sg_num)
print(sg)

🔍 Comparing Crystal Structures

PyMatGen can also help you compare two structures, either fully or partially (e.g. ignoring atomic species).

Basic Structure Comparison

frompymatgen.coreimportStructurefrompymatgen.analysis.structure_matcherimportStructureMatcherstructure1=Structure.from_file("path/to/file/my_structure.cif")
structure2=Structure.from_file("path/to/file/target_structure.cif")
matcher=StructureMatcher()
print(matcher.fit(structure1, structure2)) # Returns True or False

🛠️ You can tune the comparison with parameters like:

ltol# Length tolerance (default: 0.2)stol# Site tolerance (default: 0.3)angle_tol# Angle tolerance in degrees (default: 5)

🧱 Framework Comparison

You can also compare structures ignoring the atom types, to see if they share the same framework (e.g. Si and C in diamond structure):

frompymatgen.coreimportStructurefrompymatgen.analysis.structure_matcherimportStructureMatcher, FrameworkComparatordiamond=Structure.from_file("path/to/file/diamond.cif")
silicon=Structure.from_file("path/to/file/Si.cif")
matcher=StructureMatcher(comparator=FrameworkComparator())
print(matcher.fit(diamond, silicon)) # Should return True

🎉 Success!

If you've reached this point:

✅ PyMatGen is installed and working
✅ You can analyze symmetry and compare structures
✅ You're ready for more advanced structure analysis!

👉 Continue to Tutorial 12

Clone this wiki locally