头歌Python作业——9.1 X射线衍射曲线绘制(project)

第1关 X射线衍射曲线

import matplotlib.pyplot as plt

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 plot_xrd(data_list):
    """接收二维列表为参数,绘制曲线"""
    x = [d[0] for d in data_list]
    y = [d[1] for d in data_list]
    plt.plot(x, y)

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


第2关 X 射线衍射曲线标注

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 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 add_label():
    """增加坐标轴标识与图名"""
    plt.axhline(0, linestyle='--', color='b')
    plt.axvline(0, linestyle='--', color='r')
    plt.title("X射线衍射图谱")
    plt.xlabel("2d")
    plt.ylabel("intensity")

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


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

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射线衍射曲线多子图绘制

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()

如果此文章对你有所帮助,麻烦点个赞,谢谢~~~

点赞加关注,追新不迷路~~~

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

萌新发文啦~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值