Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1651898
  • 博文数量: 245
  • 博客积分: 10378
  • 博客等级: 上将
  • 技术积分: 2571
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 08:19
文章分类

全部博文(245)

文章存档

2013年(4)

2012年(8)

2011年(13)

2010年(68)

2009年(152)

分类: Python/Ruby

2012-11-25 15:06:05

依赖的包:
1.xlrd
2.matplot
3.numpy

读取excel文件并绘制图表
代码如下: 

点击(此处)折叠或打开

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import xlrd
  4. import os
  5. from StringIO import StringIO


  6. if __name__ == '__main__':
  7.     data = xlrd.open_workbook('D:\\tt.xls')
  8.     
  9.     plt.figure(figsize=(8,4))
  10.     plt.xlabel(u'second')
  11.     plt.ylabel(u'xxx')
  12.    
  13.     
  14.     x_index=1
  15.     
  16.     data.sheet_names()
  17.     table = data.sheets()[0]
  18.     table = data.sheet_by_index(0)
  19.     table = data.sheet_by_name(u'Sheet1')
  20.     print("Good")
  21.     COLOR_INDEX=1
  22.     INDEX_NAME=''
  23.     
  24.     ## init data
  25.     
  26.     
  27.     nrows = table.nrows
  28.     ncols = table.ncols
  29.     print("nr=%d nc=%d \n"%(nrows,ncols))
  30.     
  31.     '''
  32.     for rownum in range(table.nrows):
  33.             value = table.cell(rownum,2).value
  34.             if rownum == 0:
  35.                 print("")
  36.             else:
  37.                 try:
  38.                     value_int = int(value)
  39.                     x.append(x_index)
  40.                     y.append(value_int)
  41.                    # line = ax.plot(x_index,value_int,label="xx ",color="red",linewidth=2)
  42.                     #plt.plot(x_index,value_int,label="xx ",color="red",linewidth=2)
  43.                     print("index=%d"%x_index)
  44.                     
  45.                     #plt.plot_date(x_index,value_int)
  46.                     x_index=x_index+1
  47.                 except:
  48.                     print("error")
  49.     plt.plot(x,y,label="xx ",color="red",linewidth=2)
  50.     '''
  51.     for colnum in range(table.ncols):
  52.         x=[]
  53.         y=[]
  54.         for rownum in range(table.nrows):
  55.             value = table.cell(rownum,colnum).value
  56.             #print("nr=%d nc=%d value=%d \n"%(rownum,colnum,value ))
  57.             #print(value)
  58.             #print("rownum=%d colnum=%d "%(rownum,colnum))
  59.             if rownum == 0:
  60.                 print("")
  61.             else:
  62.                 try:
  63.                     value_int = int(value)
  64.                     x.append(x_index)
  65.                     y.append(value_int)
  66.                     #plt.plot(x_index,value_int,label=" ",color="red",linewidth=2)
  67.                     #print("index=%d"%x_index)
  68.                     
  69.                     #plt.plot_date(x_index,value_int)
  70.                     x_index=x_index+1
  71.                 except:
  72.                     print("error")
  73.         if COLOR_INDEX == 1:
  74.             COLOR_INDEX = 0
  75.             plt.plot(x,y,color="red",linewidth=2)
  76.         else:
  77.             COLOR_INDEX =1
  78.             plt.plot(x,y,color="blue",linewidth=2)
  79.         print("==============================>")
  80.        
  81.     plt.title("Test")
  82.     #plt.ylim(10,2000)
  83.     plt.legend()
  84.     plt.show()
  85.         
  86.    
  87.     pass


阅读(9320) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~