0

i'm using spyder to plot some graphe the problem is that i want to avoid the first line and convert the X axis in a format that python could plot. i founded two solution for each probleme, my file format is like this

Heure   O3  unit    alarm
11:46:35    1.054   ppb 
11:46:45    0.9767  ppb 
11:46:55    1.172   ppb 
11:47:05    1.173   ppb 
11:47:15    1.252   ppb 
11:47:25    0.813   ppb 
11:47:35    1.247   ppb 
11:47:45    1.054   ppb 
11:47:55    1.721   ppb 
11:48:05    1.098   ppb 
11:48:15    1.321   ppb 
11:48:25    0.4157  ppb 
11:48:35    0.856   ppb 
11:48:45    0.856   ppb 

first solution to avoid the first line:

f = open("filename.txt", "r")
header1 = f.readline()
for line in f:
line = line.strip()
columns = line.split()
time = columns[0]
concentration = float(columns[1])
print time, concentration

but i can not plot it because he is not accepting the format HH:MM:SS

second solution for plotting:

import matplotlib.pyplot as plt
from matplotlib.dates import strpdate2num
import numpy as np

time, val = np.loadtxt("filename.txt", 
                       usecols=(0,1), unpack=True,converters = {0: strpdate2num("%H:%M:%S")})

plt.plot_date(time, val, "r")
plt.show()

it work but i need to delete the first line manually, i'm trying to avoid this step because i will repeat this operation for several txt files.

  • Try using pandas, and it's read_table function to be more specific. Then you should be able to plot the data using column names. And you can also have a look here: http://stackoverflow.com/questions/1574088/plotting-time-in-python-with-matplotlib – Phlya Dec 03 '14 at 08:05

2 Answers2

1

With numpy you can use genfromtxt function that will let you specify the amount of first lines to ignore with the argument 'skiprows'

# Ignore first two lines 
t,a,b = np.genfromtxt(path,skiprows=2,delimiter='\t',unpack=True)
Overdrivr
  • 6,296
  • 5
  • 44
  • 70
0

thanks for answers, adding the skiprows as Overdrivr indicate it make the program work

import matplotlib.pyplot as plt
from matplotlib.dates import strpdate2num
import numpy as np

time, val = np.loadtxt("filename.txt", 
                       usecols=(0,1), sliprows=1, unpack=True,converters = {0: strpdate2num("%H:%M:%S")})

plt.plot_date(time, val, "r")
plt.show()