python 读文件数据并画图

 python 读文件数据并画图代码如下:

import pandas as pd
import matplotlib.pyplot as plt
import re
import os
import glob
import seaborn as sns
sns.set_style('white')#画布为白底
filedir = os.listdir('./')
fileref = glob.glob(r'C:\Users\admin\Desktop\算例网格文件\qblcb参考\*.txt')
x='0_iL_12'
targetfile = [name for name in filedir if x in name]
for filename in targetfile:
    keyword = re.search('x_(\d+\.\d+?|\d+?)_y',filename).group(1)
    targetfileref = [name for name in fileref if keyword in name]
    datafile = pd.read_csv(filename,sep = ' ',encoding = 'utf-8')
    datafileref = pd.read_csv(targetfileref[0], sep = '\s{3}',names='01',skiprows=[0,1,2,3], encoding = 'ANSI',engine='python')
    datafile.columns=['1','2','3','4','5','6','7','8']
    plt.plot(datafile.iloc[10:,0],datafile.iloc[10:,4],'b-')
    plt.plot(datafileref.iloc[:,0],(datafileref.iloc[:,1]+40)/100,'bo',markersize=12,markerfacecolor='none')
    plt.xlabel('time(s)')
    plt.ylabel('eta(m)')
    sub=max(datafile.iloc[10:,4])-min(datafile.iloc[10:,4])
    plt.xlim(33,39)
    plt.ylim(0.35,0.45)
    #注意以下坐标轴变化
    plt.gca().xaxis.get_major_formatter().set_powerlimits((0,1)) 
    plt.gca().yaxis.get_major_formatter().set_powerlimits((0,1)) #坐标轴科学计数法设置x轴坐标有效位数为1位
    ylm = list(plt.gca().get_ylim())#元胞转为列表
    ylm[0]*=1.3
    ylm[1]*=1.3
    plt.ylim(ylm)
    plt.xlim(left=34)
    plt.title(keyword)
    plt.grid(alpha=0.4)#添加网格,alpha控制网格线虚实
    plt.savefig(keyword+'.png')   
    plt.show()
 

1.python 读数据文件比如text或者其他形式

os.listdir()函数读文件路径下所有文件,参数为函数路径

glob.blob()函数读目录下特定格式文件,参数为文件格式后缀

具体用法如:

filedir = os.listdir('./')
fileref = glob.glob(r'C:\Users\admin\Desktop\算例网格文件\qblcb参考\*.txt')

2.从文件列表中选择包含特定字符的文件组成列表

x='0_iL_12'
targetfile = [name for name in filedir if x in name]

targetfile所有文件都包含‘0_iL_12’字符。
3.读特定包含字符的文件数据,采用正则表达式

 keyword = re.search('x_(\d+\.\d+?|\d+?)_y',filename).group(1)
#'x_(\d+\.\d+?|\d+?)_y'匹配整数或者浮点型数据

4.读文件数据

datafileref = pd.read_csv(targetfileref[0], sep = '\s{3}',names='01',skiprows=[0,1,2,3], encoding = 'ANSI',engine='python')

pd.read_csv()函数,返回类型为DataFrame类型

第一个参数为待读取目标文件,

sep参数:文件数据分隔符,本例数据为列间间隔为三个空格‘\s{3}’。如果不指定参数,则会尝试使用逗号分隔。分隔符长于一个字符并且不是‘\s+’,将使用python的语法分析器。

names参数给DataFrame列命名。用于结果的列名列表,如果数据文件中没有列标题行,就需要执行 header=None。names属性在header之前运行默认列表中不能出现重复,除非设定参数mangle_dupe_cols=True。

skiprows参数:跳过特定行读数据。需要忽略的行数(从文件开始处算起),或需要跳过的行号列表(从0开始)。

encoding参数:数据文件编码格式

engine=‘python’解决报错错误

ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
  datafileref = pd.read_csv(targetfileref[0], sep = '\s{3}',names='01',skiprows=[0,1,2,3], encoding = 'ANSI')

5.   plt.show()在plt.savefig(keyword+'.png')   下一行,否则保存图片为空白。

6. 均方根误差求和

rmse = sum([(x - y) ** 2 for x, y in zip(u,u_ref)]) / len(u)


   

  • 0
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值