how to extract data from file in python

Viewed 105

How can i plot these data in a multiline graph?

I have tried using these code but only the first value of each line is being added to the list

For my y axis I want to get the values of the appliance multiplying the values of 0.5,0,1,0(For example for line 1,(tv value = 120) I should get 24 plots which are at

y axis :60 and x axis 0

y axis 0 and x axis 1

y axis 120 and x axis 2

and so on... having a total of 24 plots(My y labels will consist of values from 0 to 2200)

For my x axis I will be ploting the values of x_labels

file format

Residents: 4
TV1:120:0.5,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0.5,0,0,0.5,1,1,1,0.5
Computer:320:1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Fridge1:250:1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Dishwasher:500:0.5,1,1,0.5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1
Blender:700:0,0,0,0,0,0,0,0,0,0,0,0,0,0.05,0,0,0,0,0,0,0,0,0,0
Fridge2:50:1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Coffee machine:2400:0,0,0,0,0,0,0,0,0,0.05,0.05,0,0,0,0,0,0,0,0,0,0,0,0,0
Kettle:2200:0.05,0,0,0,0,1,0,0,0.05,0,0.05,0.05,0,0,0,0,0.05,0,0,0,0,0.05,0.05,0
Freezer:140:1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Slow cooker:300:0,0,0,0,0,0,0,0,0,0,0,0,0.5,1,1,1,1,1,0.5,0,0,0,0,0

My code

import matplotlib.pyplot as plt
import numpy as np

x = []
y = []

x_labels = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23']

fileobj = open('file.csv','r')
for line in fileobj:
    line_s = line.strip()
    Usage = [(x) for x in line_s.split(',')]
    y.append(Usage[2])




fileobj.close()

plt.plot(y ,"o--")
plt.ylabel("usage")
plt.xlabel("time")
plt.show()

3 Answers
import matplotlib.pyplot as plt
import numpy as np

x_labels = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23']

with open(r'C:\Users\$env:UserName\Desktop\MyScripts\file.csv', 'r') as file_obj:
    for line in file_obj:
        line = line.strip('\n')
        # print(f"{line = }")
        usage = [x for x in line.split(',')]
        # print(f"{usage = }")
        number = float(usage[0].split(':')[1])
        first = [float(usage[0].split(':')[-1])]
        remaining = [float(x) for x in usage[1:]]
        # print(f"{first = }")
        # print(f"{type(first) = }")
        # print(f"{remaining = }")
        first.extend(remaining) # * number
        res = list(map(lambda x: x * number, first))
        print(f"{res = }")

plt.plot(res, "o--")
plt.ylabel("usage")
plt.xlabel("time")
plt.show()

enter image description here

When you read the file, you're getting all data along x (time) for every device. If you want plots for all devices, you can plot them as you read the data. It appears you want the devices as labels.

fileobj = open('file.csv','r')
line = fileobj.readline()
for i, line in enumerate(fileobj):
  device, wattage, h = line.split(':')
  watts = [float(x) * float(wattage) for x in h.replace('\n','').split(',')]
  # print(device, watts, wh)
  plt.plot(watts, label=str(i+1))
  # or plt.plot(watts, label=device) # if you want device as label

plt.legend()

Welcome to SO!

Lets try this!

fileobj = open('file.csv','r')
first_line = True

for line in fileobj:
    if first_line:
       first_line = False
       continue
    line_s = line.strip() # line -> Slow cooker:300:0,0,0,..
    name, id, values = line.split(':') # ['Slow cooker', '300', '0,0,0']
    usage = [int(x) for x in values.split(',')] # convert values into integers
    print(usage)
    y.append(usage)

My understanding is you are trying to plot this text data. Y is the usage values in the format of a list of lists.

Please comment otherwise. If you are not sure of what is happening, then use the print command as I have added in the above code.

Related