X射线衍射曲线绘制

该代码实现从文件中读取X射线衍射数据,找到最高的五个峰值并进行标注。首先,读取数据转换为浮点数列表,然后通过排序找到最大值,接着在图谱上标记这些峰值,并绘制XRD图谱。最后展示图形,突出显示峰值位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import matplotlib.pyplot as plt


def read_file(file):
    """读数据到列表,映射为浮点数"""
    with open(file) as data:  # 用上下文管理器打开文件,创建文件对象
        data.readline()
        txt_to_list = [list(map(float, line.strip().split())) for line in data.readlines()]  # 遍历文件
    return txt_to_list


# 找出纵坐标值最大的5个峰的坐标
def top_peak(xrd_in_list):
    """接收列表数据,返回纵标值最大的5组数据的列表"""
    sort_of_ls = sorted(xrd_in_list, key=lambda item: item[1], reverse=True)[:5]
    return sort_of_ls


def mark_peak(sort_of_ls):
    """接收排序的数据,绘制注释"""
    for x, y in sort_of_ls:
        plt.annotate(f'{y}', xy=(x, y), xytext=(+30, 0),
                     textcoords='offset points', fontsize=12,
                     arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))


def plot_xrd(xrd_in_list, sort_of_ls):
    """接收数据列表和排序的数据,绘制曲线和注释"""
    lsx = [data[0] for data in xrd_in_list]  # 创建空列表,分别容纳x,y坐标数据
    lsy = [data[1] for data in xrd_in_list]
    plt.plot(lsx, lsy, color='green')  # 绘制数据曲线,默认颜色
    plt.axhline(0, linestyle='--', color='red')
    plt.axvline(0, linestyle='--', color='blue')
    plt.title('X射线衍射图谱', fontproperties='SimHei')
    plt.xlabel('2d')
    plt.ylabel('Intensity')
    mark_peak(sort_of_ls)
    plt.show()


if __name__ == '__main__':
    filename = 'XRD_AFO.txt'
    xrd_data = read_file(filename)
    sort_of_xrd = top_peak(xrd_data)
    plot_xrd(xrd_data, sort_of_xrd)

扫描下方二维码图片,关注后了解更多精彩内容!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二十四桥_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值