用python从excel表格中读取数据生成可以放在科技论文中的图片
来源:互联网 发布:淘宝怎么清空购物车 编辑:程序博客网 时间:2024/04/28 22:42
最近在写毕业小论文,需要插入符合科技论文的数据图,其实很多的设置都是规定好的,虽然自己在excel中设置一下也不花多少时间,觉得好玩想着是不是可以用python做一个自动读取数据画图的小脚本,以后写报告也可以用得着。
其实整个思路很清楚,两个晚上的样子就搭好了大体的程序,主要用的就是xlrd的模块读取excel中的内容,用matplotlib画图。主要的时间基本耗费在线条粗细设置啦字体这些上面。
下面给出程序。
"""Created on Thu Dec 17 19:46:22 2015@author: hp-hp"""import sys,xdrlibimport osimport xlrdimport numpy as npimport matplotlib.pyplot as pltimport matplotlibfrom scipy.interpolate import interp1dfrom matplotlib.ticker import MultipleLocatorpath=os.getcwd()LineColor=['','black','red','darkblue']ll=['none','-','--','-.',':']#myfont=matplotlib.font_manager.FontProperties(fname='times.ttf',size=12)def GetDataFromExcel(file,by_index=0): data=xlrd.open_workbook(file)# print dir(data) table=data.sheets()[by_index] #print table nrows=table.nrows #print nrows,'nrows' ncols=table.ncols #print ncols,'nclos' databig=np.zeros((ncols,nrows-1)) #print databig #list_x=[] for colnum in range(ncols): listx=[] #global databig #print 'colnum=',colnum col=table.col_values(colnum) for rownum in range(1,nrows): #print'rownum=',rownum listx.append(col[rownum]) #print listx databig[colnum]=listx return databig def GetSheetNum(file): data=xlrd.open_workbook(file) return len(data.sheets()) def GetFlag(file,by_index): data=xlrd.open_workbook(file) flag=data.sheets()[by_index].name return flag def GetLabel(file,by_index): linelabel=[] data=xlrd.open_workbook(file) table=data.sheets()[by_index] nrows=table.nrows ncols=table.ncols row0=table.row_values(0) for i in range(1,ncols):# aa='$'+row0[i]+'$' aa=row0[i] linelabel.append(aa)# print linelabel return linelabel def GenerateFigure(x,labellist,flag,xmin,xmax,legendlocation): plt.figure(figsize=(8,4)) xnum=x.shape[1] xnumnew=xnum*7 ax=plt.gca()###########################画图啦################################################## for i in range(1,x.shape[0]): f=interp1d(x[0], x[i], kind='cubic') xnew = np.linspace(x[0][0], x[0][xnum-1], num=xnumnew, endpoint=True) plt.plot(xnew,f(xnew),ll[i],color=LineColor[i],linewidth=2.5) lg=plt.legend(labellist,shadow='true') for t in lg.get_texts():# print dir(t) t.set_font_properties('Times New Roman') t.set_fontweight('bold') t.set_fontsize(12) font = {'family' : 'Times New Roman', 'color' : 'black', 'weight' : 'bold', 'size' : 12, } for tick in ax.xaxis.get_major_ticks():# print dir(tick.label1) tick.label1.set_font_properties('Times New Roman') tick.label1.set_weight('bold') tick.label1.set_fontsize(12) for tick in ax.yaxis.get_major_ticks(): tick.label1.set_font_properties('Times New Roman') tick.label1.set_weight('bold') tick.label1.set_fontsize(12) wrapper=ax.spines wrapper['top'].set_linewidth(2) wrapper['bottom'].set_linewidth(2) wrapper['left'].set_linewidth(2) wrapper['right'].set_linewidth(2) for tickline in ax.xaxis.get_ticklines(): tickline.set_markeredgewidth(1.5) tickline.set_markersize(4) for tickline in ax.yaxis.get_ticklines(): tickline.set_markeredgewidth(1.5) tickline.set_markersize(4) if (flag.upper()=='VSWR'): plt.xlabel("Freq (Ghz)",fontdict=font) plt.ylabel("VSWR",fontdict=font) ax.get_legend()._loc=legendlocation if(xmin!='default' and xmax!='default'): plt.xlim(xmin,xmax) if (flag.upper()[:4]=='GAIN'): plt.xlim(-90,90) ax.xaxis.set_major_locator(MultipleLocator(30)) plt.xlabel("Theta (deg)",fontdict=font) plt.ylabel("Gain (dB)",fontdict=font) ax.get_legend()._loc=8 if (flag.upper()=='S11'): plt.xlabel("Freq (GHz)",fontdict=font) plt.ylabel("S11 (dB)",fontdict=font) ax.get_legend()._loc=legendlocation if(xmin!='default' and xmax!='default'): plt.xlim(xmin,xmax) if (flag.upper()[0:9]=='RCSVSFREQ'): plt.xlabel("Freq (Ghz)",fontdict=font) plt.ylabel("Monostatic RCS (dB)",fontdict=font) ax.get_legend()._loc=2 if(xmin!='default' and xmax!='default'): plt.xlim(xmin,xmax)# plt.savefig('01.png',dpi=75)def SaveFigure(filename,flag,dpivalue): figurename='' filenamenew=filename.split('.')[0] figurename=filenamenew+'_'+flag# print figurename plt.savefig(figurename,dpi=dpivalue)# print plt.savefig.__doc__ if __name__=='__main__': filename='jx.xlsx'# sheetnum=1 sheetnum=GetSheetNum(filename) for sheetnum in range(sheetnum): flag=GetFlag(filename,sheetnum) x=GetDataFromExcel(filename,sheetnum) labelx=GetLabel(filename,sheetnum)# lablex=["$xx$","$yy$","$zz$","$aa$"] flagx=GetFlag(filename,sheetnum) GenerateFigure(x,labelx,flagx) SaveFigure(filename,flag)# print GetFlag('vswr.xlsx',1)
因为后来想写一个ui界面,方便其他小伙伴一起用,后来发现用的Tkinter做出来的界面实在太丑,各种设置选项又太多,那样还不如直接用excel了,于是作罢了。所以程序里有些小改动,后来懒得改回去了。
最终生成的图片是这样的:
0 0
- 用python从excel表格中读取数据生成可以放在科技论文中的图片
- QTP中读取Excel表格中的数据
- java读取CSV表格数据放在实体类中的
- 读取EXCEL表格中的图片
- 一个可以从excel中读取数据并生成xml的vba程序
- 读取Excel表格中的数据
- 用java代码实现从excel表格读取数据然后写入生成国际化配置文件properties
- 读取Excel文件中的数据,并显示在MSFlexGrid表格中
- python从excel表格中获取时间格式的数据
- 用Python读取excel中的数据
- 读取Excel表格中的内容(可以读取任何版本)
- Visual C#读取Excel表格中的数据
- Visual C#读取Excel表格中的数据
- 使用java读取Excel表格中的数据 .
- C#读取Excel表格中的数据
- 使用java读取Excel表格中的数据
- java读取Excel表格中的数据
- PhpExcel读取Excel表格中的数据
- iOS 页面之间的传值总结
- hihoCoder 1261 String Problem II
- HDU 1619:Unidirectional TSP【dfs & 回忆路径】
- HandlerThread类应用
- 手机助手(一)
- 用python从excel表格中读取数据生成可以放在科技论文中的图片
- 缩写含义
- ios之核心动画(二)
- java volatile详解
- shell笔记(5):字符截取命令
- 编程小白成长记(一)
- java中没有sizeof
- awk 计算年龄
- 给一个视图添加圆角效果