Plot guides › By field

Plot XRD patterns

An XRD pattern plots diffracted intensity against the angle 2θ. To compare samples, patterns are normalised and stacked with a vertical offset, and the main reflections are labelled with their Miller indices.

Example plot xrd patterns made with matplotlib
Rendered from the code below, with example data.

When to use it

Ask for it in pltchat

Upload your file and type something like:

"stack the XRD patterns with an offset, normalise each to its max, label the main peaks"

pltchat writes the loader and the plot for your columns, runs it in your browser and shows you the code.

The matplotlib code

Self-contained with example data; swap in pd.read_csv(...) or pd.read_excel(...) for your own file.

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(32)
two_theta = np.linspace(20, 80, 3000)
peaks = {"(111)": 28.4, "(220)": 47.3, "(311)": 56.1, "(400)": 69.1, "(331)": 76.4}
rel = [1.0, 0.55, 0.3, 0.08, 0.1]

def pattern(width, shift=0.0):
    y = sum(a * np.exp(-0.5 * ((two_theta - p - shift) / width) ** 2)
            for a, p in zip(rel, peaks.values()))
    return y + 0.03 + rng.normal(0, 0.008, two_theta.size)

fig, ax = plt.subplots(figsize=(6.4, 4.5))
for i, (name, width) in enumerate([("As-deposited", 0.6), ("400 °C", 0.3), ("800 °C", 0.12)]):
    y = pattern(width)
    ax.plot(two_theta, y / y.max() + 1.1 * i, lw=0.9)
    ax.text(79.5, 1.1 * i + 0.2, name, ha="right")
for label, pos in peaks.items():
    ax.text(pos, 3.35, label, ha="center", fontsize=8)
ax.set_xlabel("2θ (°, Cu Kα)")
ax.set_ylabel("Normalised intensity (offset)")
ax.set_yticks([])
ax.set_xlim(20, 80)
plt.tight_layout()
plt.show()

Make this with your data

Upload a CSV, Excel, JSON or Parquet file. Your first plot is always free.

Related plots