1

I have some csv data in the following format.

Ln    Dr    Tag Lab    0:01    0:02    0:03    0:04    0:05    0:06    0:07    0:08   0:09
L0   St     vT  4R       0       0       0       0       0      0        0       0      0
L2   Tx     st  4R       8       8       8       8       8      8        8       8      8
L2   Tx     ss  4R       1       1       9       6       1      0        0       6      7

I want to plot a timeseries graph using the columns (Ln , Dr, Tg,Lab) as the keys and the 0:0n field as values on a timeseries graph.

I have the following code.

#!/usr/bin/env python

import matplotlib.pyplot as plt
import datetime
import numpy as np
import csv
import sys


with open("test.csv", 'r', newline='') as fin:
    reader = csv.DictReader(fin)
    for row in reader:
          key = (row['Ln'], row['Dr'], row['Tg'],row['Lab'])
          #code to extract the values and plot a timeseries.

How do I extract all the values in columns 0:0n without induviduall specifying each one of them. I want all the timeseries to be plotted on a single timeseries?

2 Answers 2

1

I'd suggest using pandas:

import pandas as pd
a=pd.read_csv('yourfile.txt',delim_whitespace=True)
for x in a.iterrows():
    x[1][4:].plot(label=str(x[1][0])+str(x[1][1])+str(x[1][2])+str(x[1][3]))

plt.ylim(-1,10)
plt.legend()

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

@atomh33Is - That did work.But my data set is much larger than I have shown I have around 200 such rows.I want a separate graph for each row. Can that be done and all such graphs output to a single PDF file for e.g ?
@liv2hak I don't see why not, it may take a while depending on your machine's spec. Use plt.savefig('yourfile.pdf') for a pdf.
I can output the graph as above to a pdf. What I want, and I am not able to figure out, is multiple graphs one for each label all written to a single pdf.
@liv2hak sounds like a different question?
can you please have a look at stackoverflow.com/questions/33713034/… ? thanks.
0

I'm not really sure exactly what you want to do but np.loadtxt is the way to go here. make sure to set the delimiter correctly for your file

data = np.loadtxt(fname="test.csv",delimiter=',',skiprows=1)

now the n-th column of data is the n-th column of the file and same for rows.

you can access data by line: data[n] or by column: data[:,n]

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.