第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()
如果此文章对你有所帮助,麻烦点个赞,谢谢~~~
点赞加关注,追新不迷路~~~