繁体   English   中英

Python:以小时,分钟和秒为单位读取文本文件; 和度数,弧分和弧秒

[英]Python: Read text file with time in hour, minutes and seconds; and angle in degree, arc minutes and arc seconds

我试图使用matplotlib从python中的文件中绘制数据。

该文件包含2列。 第一列有hour:minute:seconds 第二列有degree:arc minutes:arc seconds

对于hour:minute:seconds我使用datetime.strptime('%H:%M:%S.%f') 是否有类似的degree:arc minutes:arc seconds函数degree:arc minutes:arc seconds Python或Matplotlib中的degree:arc minutes:arc seconds

以下是数据文件的示例:

  00:06:04.8        -70:00:00.0
  00:07:01.7        -66:00:00.0
  00:14:17.7        -59:00:00.0
  00:23:00.0        -52:00:00.0
  00:23:50.3        -49:00:00.0
  00:23:54.4        -29:00:00.0
  00:23:59.4        -28:00:00.0
  00:24:03.7        -26:00:00.0
  00:24:03.8        -14:00:00.0
  00:24:03.9        +25:00:00.0 
  00:30:30.10       +30:00:00.0 

使用matplotlib.dates.datestr2num您可以轻松地将第一列转换为可绘制的数字,但我找不到第二列的函数。 但是,您可以构建一个函数来处理它:

import numpy as np

def calc_hour( str ):
    hour, min, sec = [float(i) for i in str.split(':')]
    min += sec/60.
    hour += min/60.
    return hour

calc_hour = np.vectorize( calc_hour )

def calc_deg( str ):
    deg, min, sec = [float(i) for i in str.split(':')]
    min += sec/60.
    deg += min/60.
    return deg

calc_deg = np.vectorize( calc_deg )

然后,从所谓的'tmp.txt'文件中读取数据:

values = np.loadtxt('tmp.txt', dtype=str)
hours= calc_hour( values[:,0] )
degs =  calc_deg( values[:,1] )

获得类似的东西:

hours = array([ 0.10133333,  0.11713889,  0.23825   ,  0.38333333,  0.39730556,
                0.39844444,  0.39983333,  0.40102778,  0.40105556,  0.40108333,
                0.50836111])    

degs = array([-70., -66., -59., -52., -49., -29., -28., -26., -14.,  25.,  30.])

可以绘制:

import matplotlib.pyplot as plt
plt.plot(hours,degs)

对于你的情况,给予:

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM