1

I am very new to Python

Trying to plot one column of data from multiple similar files (see below)

#0  Date-time:  20/02/2015 22:50            
#1  Recorder:   35X1078         
#2  File type:  0           
#3  Columns:    7           
#4  Channels:   4           
#5  Field separation:   1           
#6  Decimal point:  1           
#7  Date def.:  0   2       
#8  Time def.:  0           
#9  Channel 1:  Temperature(°C) Temp(°C)    3   1
#10 Channel 2:  Depth(m)    Depth(m)    2   2
#11 Reconvertion:   0           
#14 Channel 3:  Pitch(°)    Pitch(°)    1   1
#18 Channel 4:  Roll(°) Roll(°) 1   1
#19 Line color: 1   2   3   4
1 20-02-15 12:00:00 8.615 -2.64 92.3 88.8                   
2 20-02-15 12:00:01 8.615 -2.64 92.1 89.1                   
3 20-02-15 12:00:02 8.615 -2.64 92.1 87.2                   
4 20-02-15 12:00:03 8.615 -2.64 92.6 89.6                   
5 20-02-15 12:00:04 8.615 -2.64 91.9 91.5                   
6 20-02-15 12:00:05 8.615 -2.64 91.9 88.7                   
7 20-02-15 12:00:06 8.615 -2.64 92.1 89.7                   
8 20-02-15 12:00:07 8.615 -2.64 92.5 87.2                   
9 20-02-15 12:00:08 8.615 -2.66 92.1 87.6                   
10 20-02-15 12:00:09 8.615 -2.64 92.9 87.4  

Data has a number of header rows to be ignored and ignore first column

My code for plotting a single file is:

import matplotlib.pyplot as plt
import pandas as pd
import sys


print 'loading & plotting: '+sys.argv[1]+'.txt'
data=pd.read_csv(sys.argv[1]+'.txt',delimiter='\s',skiprows=17,names=['index','date','time','temp','pressure','pitch','roll'],infer_datetime_format=True)

fig,axes = plt.subplots(4,1)
data.plot('time','temp',ax=axes[0])
axes[0].set_ylabel('temperature $^\circ$C')
data.plot('time','pressure',ax=axes[1])
axes[1].set_ylabel('pressure (Bar)')
data.plot('time','pitch',ax=axes[2])
axes[2].set_ylabel('pitch $^\circ$')
data.plot('time','roll',ax=axes[3])
axes[3].set_ylabel('roll $^\circ$')
fig.suptitle('Data from Star Oddi pitch + roll sensor X1078: %s'%(data['date'][0]))

fig.savefig('X1078_uimage.png',bbox_inches='tight')
plt.show()

I know the layout of my graphs is rubbish, I can tidy that myself, but I now want to plot multiple files on to the same graphs.

0

1 Answer 1

1

You just need to repeat plot calls inside a loop while changing your data, but leaving the figure creation and saving out of the loop.

If you often deal with lists of files, glob module is good and easy.

from glob import glob
import matplotlib.pyplot as plt

files = glob("*.txt")
fig, ax = plt.subplots()

for f in files:
    print("Current file is"+f)
    #your csv loading into data   
    data.plot('time','temp',ax=axes[0])   

#outside of the for loop
plt.savefig("myplots.png")    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.