4

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()

2
  • Can you explain a bit more? Do you want the appliance name on the X axis? Say TV1 at x=0, Computer at x=1 (though 0, 1, etc wont show up). What about the multiple y values? Do you want them joined to each other (first TV entry joined with a line to first computer entry and so on)? Is each line guaranteed to have exactly same number of entries? Commented May 28, 2020 at 11:25
  • @VBB I have updated the question....If its still not clear please let me know Commented May 28, 2020 at 11:36

3 Answers 3

2

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()
Sign up to request clarification or add additional context in comments.

2 Comments

your code worked perfectly, but can you please explain to me, how did the 24 hour time appear on the x axis?
This is just implicit - to plot against the index of the y- data when no corresponding x- data is given. In other words, plt.plot(watts) is the same as plt.plot(range(len(watts)),watts).
2
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

2 Comments

I now have all values in different list, but how do I take the first value (Ex:Computer:320: How do i take the value 320), then multiply it be the values in the list(Ex:1,0,0), then plot the graph. What im trying to say is that my y labels should be like: 2200 2000 1800 1600 ...
Take the value out like this: usage[0].split(':')[1]
1

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.

2 Comments

Yep, thnks i now have all values in different list, but how do I take the first value (Ex:Computer:320: How do i take the value 320), then multiply it be the values in the list(Ex:1,0,0), then plot the graph. What im trying to say is that my y-values should be first value(320) multiplied by the other values in the list
you can do it like usage = [int(x) * int(id) for x in values.split(',')]

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.