依赖的包:
1.xlrd
2.matplot
3.numpy
读取excel文件并绘制图表
代码如下:
点击(此处)折叠或打开
importmatplotlib.pyplot as plt
importnumpy as np
importxlrd
importos
fromStringIOimportStringIO
if__name__=='__main__':
data=xlrd.open_workbook('D:\\tt.xls')
plt.figure(figsize=(8,4))
plt.xlabel(u'second')
plt.ylabel(u'xxx')
x_index=1
data.sheet_names()
table=data.sheets()[0]
table=data.sheet_by_index(0)
table=data.sheet_by_name(u'Sheet1')
print("Good")
COLOR_INDEX=1
INDEX_NAME=''
## init data
nrows=table.nrows
ncols=table.ncols
print("nr=%d nc=%d \n"%(nrows,ncols))
'''
for rownum in range(table.nrows):
value = table.cell(rownum,2).value
if rownum == 0:
print("")
else:
try:
value_int = int(value)
x.append(x_index)
y.append(value_int)
# line = ax.plot(x_index,value_int,label="xx ",color="red",linewidth=2)
#plt.plot(x_index,value_int,label="xx ",color="red",linewidth=2)
print("index=%d"%x_index)
#plt.plot_date(x_index,value_int)
x_index=x_index+1
except:
print("error")
plt.plot(x,y,label="xx ",color="red",linewidth=2)
'''
forcolnuminrange(table.ncols):
x=[]
y=[]
forrownuminrange(table.nrows):
value=table.cell(rownum,colnum).value
#print("nr=%d nc=%d value=%d \n"%(rownum,colnum,value))
#print(value)
#print("rownum=%d colnum=%d "%(rownum,colnum))
ifrownum==0:
print("")
else:
try:
value_int=int(value)
x.append(x_index)
y.append(value_int)
#plt.plot(x_index,value_int,label=" ",color="red",linewidth=2)
#print("index=%d"%x_index)
#plt.plot_date(x_index,value_int)
x_index=x_index+1
except:
print("error")
ifCOLOR_INDEX==1:
COLOR_INDEX=0
plt.plot(x,y,color="red",linewidth=2)
else:
COLOR_INDEX=1
plt.plot(x,y,color="blue",linewidth=2)
print("==============================>")
plt.title("Test")
#plt.ylim(10,2000)
plt.legend()
plt.show()
pass
本文转自 chengxuyonghu 51CTO博客,原文链接:/6226001001/1576059,如需转载请自行联系原作者