【头歌-Python】9.1 X射线衍射曲线绘制(project)第3~4关

参考教程:B站视频讲解——https://space.bilibili.com/3546616042621301

第3关:X 射线衍射曲线峰值

任务描述

本关任务:读文件中的数据绘制线图形并加绘制峰值。

相关知识

为了完成本关任务,你需要掌握:

  1. python 读取文件中的数据
  2. 使用 matplotlib 绘制图形

python 读取文件
python读取文件可以用以下函数实现:

def read_file(file):
    """ 读文件file, 返回值为二维列表,其中数据是字符串类型。 """
    with open(file, 'r', encoding='utf-8') as file:
        data_list = [line.strip().split() for line in file]
    return data_list

读为数值类型

def read_file(file):
    """ 读文件file, 返回值为二维列表,其中数据是数值类型。 """
    with open(file, 'r', encoding='utf-8') as file:
        data_list = [list(map(float,line.strip().split())) for line in file]
    return data_list

编程要求

根据提示,在右侧编辑器中补充代码,绘制 X 射线衍射峰值图。具体要求如下:

  1. 找出最高的5个峰,输出峰点坐标,
  2. 在图上标注峰点的纵坐标值,坐标值从数据中读取,参数参模板中要求。
  3. 绘制横坐标[5,25]之间的曲线图
  4. 图名为“X射线衍射图谱”
  5. 纵坐标标签为“Intensity”,横坐标标签为“2d”,
  6. 设置线颜色为“红色”,实线。
  7. 绘制红色破折线为横坐标轴,
  8. 要求中文显示正常,宋体’SimSun’,字号用默认值。

测试说明

平台会对你编写的代码进行测试:

输出示例:
在这里插入图片描述

参考代码

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimSun']
plt.rcParams['axes.unicode_minus'] = False

def read_file(file):
    """ 读文件file, 返回值为二维列表,其中数据是字符串类型。 """
    with open(file, 'r') as f:
        data_list = [list(map(eval, line.strip().split())) for line in f.readlines()[1:]]
    	return data_list

def top_five_peak(data_list):
    """参数为读文件获得的数据列表,返回纵坐标值最大的5个峰的坐标的列表,降序排序。"""
    res = sorted(data_list, key=lambda x:x[1], reverse=True)
    return res[:5]

def plot_xrd(data_list):
    """接收二维列表为参数,绘制曲线,红色实线"""
    x = [d[0] for d in data_list]
    y = [d[1] for d in data_list]
    plt.plot(x, y, 'r')

def mark_peak(peak_ls):
    """参数为峰值数据列表,在指定的坐标点加注释。注释标记点相对横坐标偏移+30,纵坐标等高,
    注释文本为峰高,即y 值,注释文本字号为12,箭头类型"->"。
    """
    for x, y in peak_ls:
        plt.annotate(f'{y}', xy=(float(x), float(y)), xytext=(+30, 0),
            textcoords='offset points', fontsize=12,
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

def add_label():
    """增加坐标轴标识与图名"""
    plt.xlim(5, 25)
    plt.axhline(0, linestyle='--', color='b')
    plt.title("X射线衍射图谱")
    plt.xlabel("2d")
    plt.ylabel("Intensity")

if __name__ == '__main__':
    data = read_file('XRD_AFO.txt')
    peak_lst = top_five_peak(data)
    plot_xrd(data)
    mark_peak(peak_lst)
    add_label()
    plt.savefig('result/result.jpg')
    plt.show()

第4关:X 射线衍射曲线多子图绘制

任务描述

本关任务:使用matplotlib绘制图形。

相关知识

为了完成本关任务,你需要掌握:

  1. python 读取文件中的数据
  2. 多子图绘图

python 读取文件
python读取文件可以用以下函数实现:

def read_file(file):
    """ 读文件file, 返回值为二维列表,其中数据是字符串类型。 """
    with open(file, 'r', encoding='utf-8') as file:
        data_list = [line.strip().split() for line in file]
    return data_list

读为数值类型

def read_file(file):
    """ 读文件file, 返回值为二维列表,其中数据是数值类型。 """
    with open(file, 'r', encoding='utf-8') as file:
        data_list = [list(map(float,line.strip().split())) for line in file]
    return data_list

编程要求

根据提示,在右侧编辑器中补充代码,绘制 X 射线衍射曲线区域图。具体要求如下:

将画布分为三个区域,上面一个区域绘制完整XRD曲线,设置x轴范围为[5, 25]; 下面两个区域各放置x轴范围为[6.7, 7.0]和x轴范围为[9.5, 10.0]的局部放大图,使用户可以清晰的查看重叠的峰的区域。
提示:
第一个子图的绘制要求与第3关相同

测试说明

平台会对你编写的代码进行测试:

输出示例:
在这里插入图片描述

参考代码

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimSun']
plt.rcParams['axes.unicode_minus'] = False

def read_file(file):
    """ 读文件file, 返回值为二维列表,其中数据是字符串类型。 """
    with open(file, 'r') as f:
        data_list = [list(map(eval, line.strip().split())) for line in f.readlines()[1:]]
    	return data_list

def top_five_peak(data_list):
    """参数为读文件获得的数据列表,返回纵坐标值最大的5个峰的坐标的列表,降序排序。"""
    res = sorted(data_list, key=lambda x:x[1], reverse=True)
    return res[:5]

def plot_xrd(data_list):
    """接收二维列表为参数,绘制曲线,红色实线"""
    x = [d[0] for d in data_list]
    y = [d[1] for d in data_list]
    plt.subplot(211)
    plt.plot(x, y, 'r')
    add_label()

def mark_peak(peak_ls):
    """参数为峰值数据列表,在指定的坐标点加注释。注释标记点相对横坐标偏移+30,纵坐标等高,
    注释文本为峰高,即y 值,注释文本字号为12,箭头类型"->"。
    """
    for x, y in peak_ls:
        plt.annotate(f'{y}', xy=(float(x), float(y)), xytext=(+30, 0),
            textcoords='offset points', fontsize=12,
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

def add_label():
    """增加坐标轴标识与图名"""
    plt.xlim(5, 25)
    plt.axhline(0, linestyle='--', color='b')
    plt.title("X射线衍射图谱")
    plt.xlabel("2d")
    plt.ylabel("Intensity")

def sub_xrd(data_list):
    """接收二维列表为参数,在第二行第1和2列绘制x值在[6.7, 7.0]和[9.5, 10]间曲线"""
    x = [d[0] for d in data_list]
    y = [d[1] for d in data_list]
    plt.subplot(223)
    plt.plot(x, y, 'b')
    plt.xlim(6.7, 7.0)
    plt.subplot(224)
    plt.plot(x, y, 'b')
    plt.xlim(9.5, 10)

if __name__ == '__main__':
    data = read_file('XRD_AFO.txt')
    peak_lst = top_five_peak(data)
    plot_xrd(data)
    mark_peak(peak_lst)
    sub_xrd(data)
    plt.savefig('result/result.jpg')
    plt.show()
  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

谛凌

本人水平有限,感谢您支持与指正

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

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

打赏作者

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

抵扣说明:

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

余额充值