使用Python绘制XRD谱图的技术问题求助
XRD谱图绘制问题解决方案
问题总结
作为Python新手,绘制13组XRD谱图时遇到以下问题:
- 如何做抗锯齿处理优化谱图外观?
- 如何添加ICDD标准卡片信息(类似Origin Pro样式)?
- 聚焦39.5°-42°范围时XRD峰展宽,如何获得尖锐峰形?
同时需要推荐XRD可视化工具。
用户原始代码(存在数据引用错误):
import pandas as pd import seaborn as sns import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import make_interp_spline file_path = "X-Ray Data of Laser Remelted Buttons.xlsx" # Replace with the actual file path excel_data = pd.ExcelFile(file_path) sheet_names = excel_data.sheet_names dataframes = {} for sheet_name in sheet_names: df = pd.read_excel(file_path, sheet_name=sheet_name, skiprows=1) dataframes[sheet_name] = df # Accessing a specific dataframe and sheet name sheet_name = sheet_names[0] df = dataframes[sheet_name] print("Sheet Name:", sheet_name) print(df.head()) fig, ax = plt.subplots() offset = 0 # Initialize the offset for sheet_name in sheet_names: data = excel_data.parse(sheet_name) x = df['X'] # Bug: 始终使用第一个sheet的数据,而非当前sheet y = df['Y (counts)'] # Bug: 同上 ax.plot(x, y + offset, label=sheet_name) # Plot the XRD data with the offset applied offset += max(y) - min(y) # Update the offset based on the range of the current data ax.set_xlabel("X Values") # Customize the X-axis label ax.set_ylabel("Y Values") # Customize the Y-axis label ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1), ncol=3) # Adjust the coordinates and ncol as per your preference ax.set_title("XRD Spectra") # Customize the plot title ax.set_yticklabels([]) ax.set_xlim(20, 95) #ax.grid(True) # Add a grid to the plot plt.show() # Display the plot
逐一解决方案
1. 抗锯齿处理优化谱图
两种简单有效的方式:
- 提高画布DPI(分辨率),让线条细节更清晰
- 在
plot函数中启用抗锯齿参数
修改后的核心代码片段:
# 提高画布分辨率,同时启用抗锯齿 fig, ax = plt.subplots(dpi=300) offset = 0 for sheet_name in sheet_names: df = dataframes[sheet_name] # 修正数据引用错误 x = df['X'] y = df['Y (counts)'] # 添加antialiased=True启用抗锯齿 ax.plot(x, y + offset, label=sheet_name, antialiased=True) offset += max(y) - min(y)
2. 添加ICDD标准卡片信息
通过绘制垂直标记线+物相标注模拟Origin的ICDD样式,步骤如下:
- 准备ICDD标准峰位数据(根据你的实际物相调整)
- 用
ax.vlines绘制峰位竖线 - 用
ax.annotate标注物相名称
示例代码:
# 示例ICDD数据:键为物相名称,值为2theta峰位列表 icdd_data = { "TiO₂ (Rutile)": [27.4, 36.1, 41.2, 54.3], "Al₂O₃": [25.5, 35.1, 43.3, 52.5] } # 绘制ICDD峰位竖线 y_max = offset # 获取所有谱图的最大Y偏移值 for phase, peaks in icdd_data.items(): # 浅灰色虚线绘制峰位,透明度降低避免遮挡谱图 ax.vlines(peaks, ymin=0, ymax=y_max, colors='gray', linestyles='--', linewidth=1, alpha=0.7) # 在第一个峰位上方标注物相名称 ax.annotate(phase, xy=(peaks[0], y_max * 1.02), ha='center', fontsize=8)
3. 消除峰展宽,获得尖锐峰形
首先修正代码中的数据引用错误(此前循环始终调用第一个sheet的数据,可能导致峰形异常),若聚焦后仍有展宽,从两点优化:
- 避免平滑插值:
make_interp_spline这类样条插值会抹平峰的尖锐度,直接绘制原始数据即可 - 补充数据点(可选):若原始数据采样稀疏,用线性插值补充点(而非样条插值)
聚焦范围的修正代码:
# 设置聚焦范围39.5°-42° ax.set_xlim(39.5, 42) offset = 0 for sheet_name in sheet_names: df = dataframes[sheet_name] x = df['X'] y = df['Y (counts)'] # 过滤目标范围内的数据 mask = (x >= 39.5) & (x <= 42) x_filtered = x[mask] y_filtered = y[mask] # 线性插值补充点(可选,根据数据密度决定是否启用) x_new = np.linspace(x_filtered.min(), x_filtered.max(), 1000) y_new = np.interp(x_new, x_filtered, y_filtered) ax.plot(x_new, y_new + offset, label=sheet_name, antialiased=True) offset += max(y_filtered) - min(y_filtered)
推荐XRD可视化工具
- pyXRD:专为XRD数据设计的分析与可视化库,支持物相匹配、峰拟合,贴合科研需求
- diffpy-cmi:衍射数据分析工具集,可处理XRD、PDF等数据,可视化功能专业
- MTEX:专注于织构分析的衍射工具,适合多晶材料XRD数据可视化
- Plotly:交互式绘图库,可生成可缩放、悬停查看数据的XRD谱图,便于展示分享
内容的提问来源于stack exchange,提问作者huseyin_hzl
相关产品推荐
相关产品推荐

