Exporting XRD as csv or txt to plot with experimental results


I’m trying to get XRD values to plot them with my experimental results. So, I used the code available in MP page, and that’s ok. But, I want to export my pattern calculated results (in the format <class ‘pymatgen.analysis.diffraction.core.DiffractionPattern’> ) to use these values with and in another programs. But, I’m getting some difficulties. Can anyone help me? Is this possible to do? If not, how can I download a XRD table (like a column with x and a column with y) from MP page?

Thank you!

You can use pandas to put the x, y coordinates into a DataFrame and then export that to a file.

I made some example code for exporting to a CSV file. If you want to export to other file types (like .xy, you can change the delimiter).

import pandas
from pymatgen.analysis.diffraction.xrd import XRDCalculator

structure = #### insert reference to your pymatgen Structure object here

c = XRDCalculator(wavelength="CuKa")

diffraction = c.get_pattern(structure)
x, y = diffraction.x, diffraction.y

df = pandas.DataFrame({"2theta": x, "I": y})
df.to_csv("pattern.csv", index=False)