python读取excel文件,绘制成图表。
本代码依赖的包:
1,xlrd
2,matplot
3,numpy
代码:读取excel文件并绘制图表
import matplotlib.pyplot as plt
import numpy as np
import xlrd
import os
from StringIO import StringIO
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)
'''
for colnum in range(table.ncols):
x=[]
y=[]
for rownum in range(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))
if rownum == 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")
if COLOR_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