-1

I am writing a program to read data from a txt file:

[2.8389999866485596, 2.8459999561309814, inf, 0.3540000021457672, 0.3070000112056732, 0.28700000047683716, 0.296999990940094, 0.29600000381469727]

There is no y-axis for this graph, currently this is the code that I have written:

import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def animate(i):
    f = open('sample_data.txt', 'r').read()
    lines = f.split('\n')
    xs=[] 
    ys=[]

    for line in lines:
        if len(line) > 1:
            x,y = line.split(',')
            xs.append(float(x))
            ys.append(float(y))

    ax1.clear()
    ax1.plot(xs,ys)

ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()

But have many errors.

Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75
Gary
  • 11
  • 2
  • 5
  • 1
    "_But have many errors_" Hi, what are the errors? (Also since you're new here on StackOverflow, do check out the [tour] and [ask].) – TrebledJ Mar 08 '19 at 09:12
  • 1
    "there is no y-axis for this graph" A 1 dimensional graph ? – JeffUK Mar 08 '19 at 09:13
  • possible duplicate of https://stackoverflow.com/questions/11874767/how-do-i-plot-in-real-time-in-a-while-loop-using-matplotlib – Vaibhav gusain Mar 08 '19 at 09:28
  • @JeffUK its just a stream of data gathered from an instrument, with some floating values and also inf – Gary Mar 11 '19 at 03:48

1 Answers1

0

Can you try the following:

def animate(i):
    f = open('sample_data.txt', 'r').read().replace('\n', '')
    ys = list(map(float, f.split(',')))
    # lines = f.split('\n')
    # xs=[]
    # ys=[]
    # for line in lines:
        # if len(line) > 1:
            # x,y = line.split(',')
            # xs.append(float(x))
            # ys.append(float(y))
    ax1.clear()
    ax1.plot(ys)

To get ranges from the txt file:

fp = r"python .txt"
with open(fp, 'r') as infile:
    data = infile.read()

for val in data.split('\n'):
    if val.startswith('ranges'):
        print(list(map(lambda x: x.strip(), val.split(':')[1].strip().replace('[', '').replace(']', '').split(','))))
        print()

It will print all the text inside the [] from each line that starts with ranges.

Jeril
  • 7,858
  • 3
  • 52
  • 69
  • Hi Jeril, thanks for the reply, however the recommended codes does not work. i still have error as the code is unable to read the value of inf. Also is it possible to write a program code without a y-axis? – Gary Mar 11 '19 at 03:52
  • you can use plot(array) – Jeril Mar 11 '19 at 04:37
  • Sorry Jeril, but can you be more specific, i am new to python, but is tasked to read a data text file and plot a live graph – Gary Mar 11 '19 at 06:26
  • Hi Jeril, thank you so much the program is working properly now. Sorry but will you be able to explain the theory behind your coding. Because i realize the code has been shortened – Gary Mar 11 '19 at 08:47
  • Since there is only a single line there was no need for the line `lines = f.split('\n')`, the `replace('\n', '')` is sufficient. Then I was splitting the line using `comma` which gives a `list of strings`, which I am converting to float using the `map` function. Finally plotting the graph using the y-axis – Jeril Mar 11 '19 at 08:53
  • Hi Jeril, thanks so much for your assistance. Similarly, if i want to upgrade my code to be able to read this set of datas (refer to next comment). But i only want to extract the "ranges" section to plot a similar graph live, is it possible? – Gary Mar 13 '19 at 00:42
  • header: seq: 7089 stamp: secs: 1539220899 nsecs: 26012152 frame_id: "laser" angle_min: -2.35619449615 angle_max: 2.35619449615 angle_increment: 0.0174532923847 time_increment: 0.000185185184819 scan_time: 0.0666666701436 range_min: 0.0500000007451 range_max: 10.0 ranges: [2.8380000591278076, 2.8459999561309814, 0.3799999952316284, 0.3630000054836273, 0.3100000023841858, 0.29499998688697815, 0.2879999876022339, 0.28999999165534973, 0.29499998688697815, 0.28700000047683716, 0.28999999165534973, 0.30799999833106995, 0.3540000021457672,] – Gary Mar 13 '19 at 00:45
  • is that a dictionary / json – Jeril Mar 13 '19 at 04:11
  • i am not too sure, it is a data gathered from an instrument and passed on to me in a .txt document. – Gary Mar 13 '19 at 06:22
  • Currently, the coding only allows me to plot the graph if i extract the "ranges" data out manually, but is it possible to read the original .txt file and do the same thing – Gary Mar 13 '19 at 06:23
  • Yes sure, but the .txt file have too much value to paste here. Any email that i can send to you? – Gary Mar 14 '19 at 00:44
  • Can you share it some drive – Jeril Mar 14 '19 at 04:20
  • Yes sure i can share it in a google drive, do you have a gmail? – Gary Mar 14 '19 at 09:33
  • Okay will look at it – Jeril Mar 15 '19 at 04:37
  • Is that a log file? – Jeril Mar 15 '19 at 04:50
  • What do you mean log file Jeril – Gary Mar 18 '19 at 01:24
  • It is a data file generated by a measuring instrument, most probably a sensor – Gary Mar 18 '19 at 01:29
  • The file that was shared, was it the exact one, or are there any brackets in front and last? – Jeril Mar 18 '19 at 05:19
  • I have modified the solution, kindly check – Jeril Mar 18 '19 at 06:42
  • Hi jeril, unable to run the program as there is error with the fp not defined – Gary Mar 18 '19 at 07:03
  • `fp` is the file path of the `.txt` file – Jeril Mar 18 '19 at 07:07