【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
本文轉自 chengxuyonghu 51CTO博客,原文鏈接:http://blog.51cto.com/6226001001/1576059,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的【python】 读取Excel文件并绘制图表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 清除BSS段的一般做法
- 下一篇: Python3 django2.0 字段