使用matplot绘图 @python

1. 使用csv 模块读取数据

2. 定义label

3. 绘图,调参

#!/usr/bin/env python
# coding=utf-8

import sys
import matplotlib.pyplot as plt
import xlrd
import csv
from datetime import datetime

reload(sys)
sys.setdefaultencoding('utf-8')

def load_data(fname='./data.xlsx'):
    wb = xlrd.open_workbook(fname)
    print wb.sheet_names()
    sheet =wb.sheet_by_index(0)
    res = []
    for i in range(1,7): #选取6行
        rows = sheet.row_values(i)
        print 'i',rows
        res.append(map(float,rows.split()))
    return res


def load_data2(fname='./data.csv'):
    reader = csv.reader(open(fname))
    res = []
    for line in reader:
        if reader.line_num == 1:
            continue
        res.append(line)
    return res

def floatrange(start,stop,steps):
    return [start+float(i)*(stop-start)/(float(steps)-1) for i in range(steps)]


def draw_picture(data=[],label=[]):
    x = map(lambda x:0.1*x,range(-12,14,2))

    fig = plt.figure()
    ax = fig.add_subplot(111)
    for i in range(len(data)):
        ax.plot(x,data[i],label=label[i],linewidth=1.5)
    
    datestr = datetime.now().strftime('%Y-%m-%d')
    ax.set_title('draw_picture demo\n\n%s\n'%(datestr))
    ax.legend()
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_xlim(-1.3,1.3,0.2)
    plt.show()
    


data = load_data2()
label = ['line1','line2','c','d','e','f']
draw_picture(data,label)

posted on 2015-11-14 12:35  星空守望者--jkmiao  阅读(1125)  评论(0编辑  收藏  举报