import os
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np
from pylab import *
srcpath1=r'C:\Users\user\Documents\F1-21\new.txt'
srcpath2=r'C:\Users\user\Documents\F1-21\f21.txt'
def openreadtxt(file_name):
data = []
with open(file_name, 'r') as file:
file_data = file.readlines() # 读取所有行
for row in file_data:
tmp_list = row.split('\n') # 去掉换行符
tmp_list = row.split()
tmp = [float(x) for x in tmp_list]
data.append(tmp) # 将每行数据插入data中
return data
if __name__ == "__main__":
data_new=openreadtxt(srcpath1)
data_f21=openreadtxt(srcpath2)
xs=np.linspace(0.02,0.32,16)
ys_new=np.zeros(16)
data_2k=[]
data_4k=[]
data_6k=[]
for i in range(len(data_new)):
ys_new+=np.array(data_new[i])
ys_new=ys_new/len(data_new)
for i in range(21):
temp_2k = np.zeros(16)
temp_4k = np.zeros(16)
temp_6k = np.zeros(16)
for j in range(9):
if j<3:
temp_2k+=np.array(data_f21[i*9+j])
elif j<6:
temp_4k+=np.array(data_f21[i*9+j])
else:
temp_6k+=np.array(data_f21[i*9+j])
data_2k.append(temp_2k/3)
data_4k.append(temp_4k/3)
data_6k.append(temp_6k/3)
fig=plt.figure(figsize=(5,4))
plt.rcParams['xtick.direction'] = 'in' # 将x周的刻度线方向设置向内
plt.rcParams['ytick.direction'] = 'in' # 将y轴的刻度方向设置向内
# plt.rcParams['font.family'] = ["Times New Roman"] # 字体设置为Times NewRoman
plt.rcParams['font.sans-serif'] = ['SimHei']
clist=['blue','red','green','black','slategray','lime','gold','purple','green','cyan']
markerlst=['o','*','x','>','<','^','D','d','1','2','3','4']
linestylelst=['-','--','-.',':','-','--','-.',':','-','--','-.',':']
labellst=['CCL-HL832NSF','CCL-HL832NX','CCL-HL972LF','R-1515W(T)','R-1515W','R-1515A(H)','DS-7409HGB(JE)','DS-7409HGB(ZLEM)','MCL-E-700G','MCL-E-705G','MCL-E-770G','MCL-E-679FG','MCL-HS200','SI13U','SI05NR','SI07NR','SI07NR(LC)','SI10NFK','MCL-E-795G-LH','MCL-E-795G','MCL-E-705G-LH',]
plt.plot(xs, ys_new,c=clist[9], marker=markerlst[10], markersize='10', linewidth='3', linestyle=linestylelst[10], label='New')
for i in range(17):
if i==7:
continue
plt.plot(xs, data_6k[i], c=clist[i%10], marker=markerlst[i%12], markersize='10', linewidth='3', linestyle=linestylelst[i%12], label=labellst[i])
font1 = {'family': 'Times New Roman', 'weight': 'normal', 'size': 16}
# 图例展示位置,数字代表第几象限
plt.legend(loc=4,prop=font1,ncol=3,framealpha=0.5)
plt.xticks(xs)
plt.yticks(np.linspace(0,0.16,11))
plt.grid(True, linestyle='--', alpha=0.5)
plt.xlabel("到钻尖的距离(mm)", fontdict={'size': 16})
plt.ylabel("钻头外径(mm)", fontdict={'size': 16})
# plt.title("孔限为6000时的外径磨损", fontdict={'size': 20})
fig.autofmt_xdate()
plt.show()
figure1 = fig.get_figure() # 获取图形
figure1.savefig("C:\\Users\\user\\Documents\\1.jpg", dpi=300)
C:\Users\user\Documents\F1-21\new.txt文件存储新钻数据:每一行为为1根的数据,每根测量16个点,故为16列,每3行为1个参数的结果
C:\Users\user\Documents\F1-21\f21.txt文件存储钻后数据:每一行为为1根的数据,每根测量16个点,故为16列,每3行为1个参数的结果
这段代码是一个用于处理数据并绘制图形的脚本。它首先读取两个文本文件的数据,然后根据这些数据生成图表。
其中,openreadtxt()
函数用于打开文本文件并读取数据。它逐行读取文件内容,并将每行数据添加到列表中。
在if __name__ == "__main__":
条件语句之后,脚本从两个文本文件中读取数据并进行处理。
接下来,使用np.linspace()
函数生成横坐标数组 xs
,并创建一个长度为 16 的全零数组 ys_new
。
接下来的循环将对数据进行处理和计算。一部分数据来自于 data_new
列表,通过遍历该列表累加数据并求平均值,得到 ys_new
数组。
另一个循环用于处理来自 data_f21
列表的数据。它按照一定规则将数据累加并求平均值,最终得到 data_2k
、data_4k
和 data_6k
三个列表。
在图表绘制部分,plt.plot()
函数用于绘制折线图。循环中的 plot()
函数会以不同的样式和颜色绘制不同的曲线。
plt.legend()
函数用于添加图例。
然后,使用一系列函数来设置图表的各种属性,例如:刻度方向、字体、坐标轴范围、网格等。
最后,使用 fig.get_figure()
获取图形对象,并通过 savefig()
函数将图表保存为图片。