Plotting of strings containing same number

Dear all,

I have a file like containing data . The datas are sequenced like following.

,
00:00:08 3.78E-7
00:02:10 3.78E-7
00:05:00 3.78E-7

…,

and so on . The right hand side is same for the all values and the left side is updating .The left hand side indicates time. I need to plot these. The time vs the pressure.

The program is following.

  
def main():


second=[]
index=[]
y=[]
x=[]
dG=[]
avg=[]
numbers=[]
with open(r'C:\Users\sarad\leybold\230623.log','r') as line: 
table = defaultdict(dict)
for line in line:
  if line:
    entry = line.strip()
    if ':' in entry:
        t = entry
   # print (t)
    for item in t:
         t1,t2=t.split(" ")
    print(t1)
    table[t1].({t1,float(t2)})
    df=pd.DataFrame(table).T     
    df.plot()
main()  

The result is not coming as expected.
Could you give some idea ?

Thank you

When you say “The result is not coming as expected.”, could you give an indication of what result you’re seeing vs. what result you’re expecting?

I am expecting a output file like following.

00:00:08 3.78E-08
00:00:20 3.78E-08
00:02:50 3.78E-08
… continue…

and a simple plot of them…

But the output i am getting

                 15:19:37      15:20:07      15:20:37  ...      23:58:58      23:59:28      23:59:58

15:19:37 7.8E-7 7.800000e-07 NaN NaN … NaN NaN NaN
15:20:07 7.8E-7 NaN 7.800000e-07 NaN … NaN NaN NaN
15:20:37 7.8E-7 NaN NaN 7.800000e-07 … NaN NaN NaN
15:21:07 7.8E-7 NaN NaN NaN … NaN NaN NaN
15:21:37 7.8E-7 NaN NaN NaN … NaN NaN NaN
… … … … … … … …
23:57:58 9.4E-7 NaN NaN NaN … NaN NaN NaN
23:58:28 9.4E-7 NaN NaN NaN … NaN NaN NaN
23:58:58 9.4E-7 NaN NaN NaN … 9.400000e-07 NaN NaN
23:59:28 9.4E-7 NaN NaN NaN … NaN 9.400000e-07 NaN
23:59:58 9.4E-7 NaN NaN NaN … NaN NaN 9.400000e-07

and it is not giving any plot …

The plot should be a straight line.

Thank you

For some reason, I’m not seeing the complete picture here; I’ll explain…

Rather than rewrite your script in a way that I would write it, I’d like to work with what you have, so that I can recreate the output that you are seeing, then try and apply a fix for you, but there are issues:

  1. As is, that script can’t possible run, because the indentation is not correct.

  2. Even if I correct (1) for you, this line of code…
    table = defaultdict(dict)
    … seems to be a call to a function that has not been disclosed.

  3. This line of code…
    table[t1].({t1,float(t2)})
    … seems to be not syntactically correct, and I’m unsure what the meaning is.

If I’m missing some fundamental knowledge here, I’ll side-line and wait for someone else to come in and help, but I do have a few years of Python coding experience, so there’s that.

I am sorry … There was some bug in the code previously. I fixed them.
The code is following now.

import pandas as pd
from collections import defaultdict
import matplotlib.pyplot as plt
from matplotlib import interactive


def main():
   with open(r'C:\Users\sarad\leybold\230623.log','r') as line: 
    table = defaultdict(dict)
    for line in line:
      if line:
        entry = line.strip()
        if ':' in entry:
            t = entry
        for item in t:
             t1,t2=t.split(" ")
             table[t].update({t1:float(t2)})
    df=pd.DataFrame(table).T
    print(df)   

main()

And the output is that i shared previously.

Thank you.

Thank you. We now have something with which to work. I have to put this on hold at my end for now, but maybe someone else will pick this up.

What I can see is that given the data…

00:00:08 3.78E-7
00:02:10 3.78E-7
00:05:00 3.78E-7

… there is a mismatch between the pandas DataFrame, which is 3x3 and the ‘table’, which is a 3x2, which is why we’re seeing NaN and no plot with df.plot()


@saradindu

As I’ve used numpy more than I’ve used pandas, I’ve coded this, which may or may not be of any help, but it works for data sample that you’ve provided:

import matplotlib.pyplot as plt
import numpy as np
t, v = 0, 1
time = []
values = []

file_path = ''  # you'll need your own file path in here


def main():
    with open(file_path, mode='r', encoding='UTF-8') as read_line:
        for line in read_line:
            entry = line.split()
            time.append(entry[t])
            values.append(float(entry[v]))
    xpoints = np.array(time)
    ypoints = np.array(values)
    plt.plot(xpoints, ypoints)
    plt.show()

main()