利用python的matplotlib处理计算数据

#!/usr/bin/python

# -*- coding: UTF-8 -*-

 

import numpy as np

import matplotlib.pyplot as plt

import csv

import unicodedata

 

#=========================================

#该段代码来支持在图片中显示中文

plt.rcParams['font.sans-serif']=['SimHei']

plt.rcParams['axes.unicode_minus']=False

#=========================================

 

#======================================

#本段代码引用自:http://www.runoob.com

#函数功能:判断是否为数字

#======================================

def is_number(s):

    try:

        float(s)

        return True

    except ValueError:

        pass

 

    try:

        unicodedata.numeric(s)

        return True

    except (TypeError, ValueError):

        pass

 

    return False

#========================================

 

x=[]

velocity=[]

 

#读取csv文件,当然也可以读取类似txt之类的文件

with open(r"E:\work\1\test.csv","r") as csvfile:

      #指定分隔符为",",当然也可以指定":","."之类的分隔符

      plots=csv.reader(csvfile,delimiter=",")     

      #循环读取到的文件

      for row in plots:     

            #==========================

            #为了跳过文件前面的非数据行           

            if plots.line_num <= 6:

                  continue

            #==========================

            #判断读入的是否为数字

            if is_number(row[0]) and is_number(row[3]):

                  x.append(float(row[0]))

                  velocity.append(float(row[3]))

 

#记得在出现汉字的时候,一定要用unicode,写法u"汉字"

plt.plot(x, velocity, label = u"1")

 

#设置x坐标显示的变量

plt.xlabel("x")

#设置y坐标显示的变量

plt.ylabel("Velocity")

 

#设置legend的位置和legend是否被边框包围

#frameon属性为true时有边框

plt.legend(loc="best",frameon=False)

 

#显示最后生成的图

plt.show()


posted @ 2016-10-29 21:09  硫酸亚铜  阅读(505)  评论(0编辑  收藏  举报