日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【python】 读取Excel文件并绘制图表

發布時間:2025/3/19 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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文件并绘制图表的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。