简体   繁体   中英

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

I am trying to plot data from a file in python using matplotlib .

The file contains 2 columns. The first Column has hour:minute:seconds . The second column has degree:arc minutes:arc seconds .

For hour:minute:seconds I am using datetime.strptime('%H:%M:%S.%f') . Is there any similar function for degree:arc minutes:arc seconds in Python or Matplotlib?

Here is an example of the data file:

  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 

Using matplotlib.dates.datestr2num you could easily convert your first column to plottable numbers, but I did not find a function for your second column. You can build a function to process that, though:

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 )

Then, read you data from a supposed 'tmp.txt' file:

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

Getting something like:

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.])

That can be plotted:

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

For you case, giving:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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