import pandas from matplotlib import pyplot as plt from matplotlib.font_manager import FontProperties import os import numpy get_path=lambda p:os.path.join(os.path.dirname(__file__),'../result/%s'%p) #get_path=lambda p:os.path.abspath(os.path.join(os.path.dirname(__file__),p)) plt.title('start app time test') #设置折线图标题 plt.xlabel('build',fontsize=5) #设置x标注 plt.ylabel('time',fontsize=5) plt.xticks(fontsize=4) #x字体 plt.yticks(fontsize=4) #plt.subplot(figsize=(10,5) #oe=Operation_excel(file) def save_png(excel_file,out_file,out_excel,png_key=None): df=pandas.read_excel(get_path(excel_file)) #读取excel表格 df=df[df['时间/s']!='error_log'] #过滤掉“时间/s”这一列为error_log的行 if png_key==None: df=df[df['帐号类型'] == 'logout'] #筛选出帐号类型为logout的数据 df['时间/s']=df['时间/s'].astype('float') #转换“时间/s”这一列数据类型 print(df.dtypes) #打印出表格数据第一列 dre=pandas.pivot_table(df,index=['机型','帐号类型'],columns='build',values='时间/s') #已机型,帐号类型分类,筛选出不同build的时间/s的平均值 dfe=pandas.pivot_table(df,index=['机型','帐号类型'],columns='build',values='时间/s',aggfunc=numpy.var) dre.to_excel(get_path(out_excel)) #保存转换过得表格 dfe.to_excel(get_path('var'+out_excel)) # print(dre.index.values) # print(dre.values) devices=[] x=[i for i in dre.dtypes.index] if png_key is None: plt.figure(figsize=(3*len(x),6)) #设置x轴长度,各个元素乘以3 plt.title('android start time static',FontProperties='STKAITI') sl=set([i[0] for i in dre.index.values]) for s in sl: print(s) if png_key is not None: plt.figure() plt.xticks(fontsize=4) plt.yticks(fontsize=4) for i in dre.index.values: if s in i: y = [] devices.append(i) print(i) data = dre.loc[i,x] #index为i的所有x值 for j in data: y.append(j) print(y) plt.plot(x,y,label=i,linewidth=3) plt.legend() if png_key is not None: plt.savefig(get_path(s+out_file), dpi=800) if png_key is None: plt.savefig(get_path(out_file),dpi=800)
pandas excel处理数据和生成折线图
最新推荐文章于 2023-03-27 19:49:34 发布