Python科学计算国网实例 发表于 2017-03-21 | 分类于 Python 一个国家电网的实例: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970"""1-导入Excel文档2-获得每一列数据3-第一列作为X轴4-2~17列作为Y的数据"""import chch.set_ch()from collections import OrderedDictimport xlrdimport matplotlib.pyplot as pltimport matplotlib as mplimport numpy as npimport scipyfrom scipy.interpolate import splinefrom matplotlib import tickerfont = { 'family':'STFangsong', 'size':12 }mpl.rc('font',**font)mpl.rc('figure',autolayout = True,facecolor = 'white')#figure自动使所有内容全部自适应出现在整个图像内mpl.rc('axes',facecolor = 'white',grid = True)#导入表格,获取sheetpath = "/Volumes/大白菜/研究生研究课题工作/国网报告整理/20170902严主任数据/实验数据/16路微水变化对比.xlsx"book_temp = xlrd.open_workbook(path)sheetName = book_temp.sheet_names()#建立每一列的数组,数组的元素是1~17列的数据sheet1 = book_temp.sheet_by_name(sheetName[0])col_name = []for i in range(16): col_name.append('col_data_'+str(i)) col_name[i] = sheet1.col_values(i)[1::15]#列出X轴的数据x = np.array([x for x in range(len(col_name[0]))])xnew = np.linspace(x.min(),x.max(),3000)#Y轴的数据,使用有序字典y = OrderedDict()for i in range(15): y[i] = col_name[i+1]ynew = OrderedDict()for i in range(15): ynew[i] = spline(x,y[i],xnew)#曲线平滑处理#进行绘图fig,ax = plt.subplots(1,1)for i in range(15): ax.plot(xnew,ynew[i],label = str(i+1)+"号")# plt.xlabel("时间")plt.ylabel("微水($\mu L/L$)")plt.legend(loc = 0,framealpha = 0.5,fontsize = 8,frameon = False)#修改legend的大小plt.title("16路微水变化对比图")#改变X轴坐标的显示import matplotlib.ticker as tickertick_spacing = 15plt.xticks(x, col_name[0], rotation=15)#改变X轴显示的内容ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))#改变X轴显示的密度fig.set_size_inches(6.3,3.5)plt.savefig('./images/%s.pdf'%(ax.get_title()),dpi = 300,bbox_inches=None, pad_inches=0.1)plt.savefig('./images/%s.png'%(ax.get_title()),dpi = 300,bbox_inches=None, pad_inches=0.1)plt.show() 打赏 微信支付 支付宝