How do I use pymatgen in python to visualize.cif files

from pymatgen.io.cif import CifParser
from pymatgen.plotting import BSPlotter
import matplotlib.pyplot as plt

parser = CifParser(“mp-6127.cif”)
structure = parser.get_structures()[0]

analyzer = SpacegroupAnalyzer(structure)
structure = analyzer.get_primitive_standard_structure()

plotter = BSPlotter(structure)
plotter.show()

plt.savefig(“structure.png”)

i can‘t discover the problem

Hi @xiaowangfeng, BSPlotter is used to plot band structures and not crystal structures. Within pymatgen, you can plot crystal structures with pymatgen.vis.structure_chemview.quick_view or pymatgen.vis.structure_vtk.StructureVis.

when i use :
from pymatgen.io.cif import CifParser
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.vis.structure_vtk import StructureVis
import matplotlib.pyplot as plt

parser = CifParser(“mp-6127.cif”)
structure = parser.parse_structures()[0]

analyzer = SpacegroupAnalyzer(structure)
structure = analyzer.get_primitive_standard_structure()

struct_vis = StructureVis()
struct_vis.set_structure(structure)
struct_vis.show()

UserWarning: The default value of primitive was changed from True to False in Deprecate `CifParser.get_structures()` in favor of new `parse_structures` in which `primitive` defaults to `False` by janosh · Pull Request #3419 · materialsproject/pymatgen · GitHub. CifParser now returns the cell in the CIF file as is. If you want the primitive cell, please set primitive=True explicitly.
warnings.warn(
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.

what can i do to solve it

The UserWarning is just letting you know that the structure you get from CifParser.get_structures will not necessarily be primitive. You can ignore this warning because you later call analyzer.get_primitive_standard_structure

For the OpenMP warnings, it looks like you’re running this explicitly parallelized over multiple cores? Is that right and if so, why? There’s no multi-core parallelization built into these particular functions

You probably want to set the environment variable OMP_NUM_THREADS=1 in your shell before running this script

I modified it as follows:

from pymatgen.io.cif import CifParser
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.vis.structure_vtk import StructureVis
import matplotlib.pyplot as plt
import os
os.environ[“OMP_NUM_THREADS”] = “1”

parser = CifParser(“mp-6127.cif”)
structure = parser.parse_structures()[0]

analyzer = SpacegroupAnalyzer(structure)
structure = analyzer.get_primitive_standard_structure()

struct_vis = StructureVis()
struct_vis.set_structure(structure)
struct_vis.show()

But he still didn’t work

UserWarning: The default value of primitive was changed from True to False in Deprecate `CifParser.get_structures()` in favor of new `parse_structures` in which `primitive` defaults to `False` by janosh · Pull Request #3419 · materialsproject/pymatgen · GitHub. CifParser now returns the cell in the CIF file as is. If you want the primitive cell, please set primitive=True explicitly.
warnings.warn(
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.

what can i do to solve it

what can i do to visualize .cif files

This should be an integer-valued 1, not a str:
os.environ["OMP_NUM_THREADS"] = 1

Which versions of pymatgen, numpy, and scipy are you using? Upgrading these packages (pip install --upgrade numpy) might solve this

i’m sorry that it still didn’t work.

from pymatgen.io.cif import CifParser
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.vis.structure_vtk import StructureVis
import matplotlib.pyplot as plt
import os
os.environ[“OMP_NUM_THREADS”] = 1
parser = CifParser(“mp-6127.cif”)
structure = parser.parse_structures()[0]

analyzer = SpacegroupAnalyzer(structure)
structure = analyzer.get_primitive_standard_structure()

struct_vis = StructureVis()
struct_vis.set_structure(structure)
struct_vis.show()

Traceback (most recent call last):
File “D:\Anaconda\envs\DL\lib\site-packages\IPython\core\interactiveshell.py”, line 3550, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 1, in
runfile(‘E:/Pycharm_project/daoru.py’, wdir=‘E:/Pycharm_project’)
File “D:\PyCharm\PyCharm Community Edition 2020.1.3\plugins\python-ce\helpers\pydev_pydev_bundle\pydev_umd.py”, line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File “D:\PyCharm\PyCharm Community Edition 2020.1.3\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py”, line 18, in execfile
exec(compile(contents+“\n”, file, ‘exec’), glob, loc)
File “E:/Pycharm_project/daoru.py”, line 7, in
os.environ[“OMP_NUM_THREADS”] = 1
File “D:\Anaconda\envs\DL\lib\os.py”, line 684, in setitem
value = self.encodevalue(value)
File “D:\Anaconda\envs\DL\lib\os.py”, line 742, in check_str
raise TypeError("str expected, not s type(value).name)
TypeError: str expected, not int

my numpy ==1.26.4 pymatgen ==2024.2.23 scipy ==1.11.4