Mean function python, doing average and add stdev in a graph

56 views Asked by At

i've a problem in python, i hope you can help me to solve it. I've a list of data, like the following:

01/31/2021 08:51:41.222 5777 40.26
01/31/2021 08:51:41.441 5802 40.26
01/31/2021 08:51:41.644 5786 40.26
01/31/2021 08:51:41.847 5793 40.29
01/31/2021 08:51:42.050 5776 40.29
01/31/2021 08:51:42.269 5798 40.29
01/31/2021 08:51:42.456 5791 40.26
01/31/2021 08:51:42.660 5800 40.26
01/31/2021 08:51:42.878 5781 40.26
01/31/2021 08:51:43.081 5793 40.29
01/31/2021 08:51:43.300 5795 40.29
01/31/2021 08:51:43.519 5799 40.29

My aim is to save data just in a chosen range of time and plot this data. That's what i've coded right now:

import numpy as np
import matplotlib.pyplot as plt
import datetime as dt
import os

os.chdir('C:\\Users\\Nic\\Desktop')

filename = "20"  

with open(filename + ".txt") as file:
    lines = file.readlines()[10:]
    date = [line.split()[0] for line in lines]
    hours =[line.split()[1] for line in lines]
    rad = [float(line.split()[2]) for line in lines]

starthour = "0:00"
endhour = "00:51"

date_str = [a + " " + b for a, b in zip(date, hours)]

data_obj = [dt.datetime.strptime(d, "%m/%d/%Y %H:%M:%S.%f") for d in date_str]

date_start = dt.datetime.strptime(date[0] + " " + starthour, "%m/%d/%Y %H:%M")

date_end = dt.datetime.strptime(date[0] + " " + endhour, "%m/%d/%Y %H:%M")

rad = [r for r, d in zip(rad, data_obj) if (d>=date_start) & (d<=date_end)]

data_obj = [d for d in data_obj if (d>=date_start) & (d<=date_end)]

plt.plot(data_obj, rad, color = "turquoise", label = "dati", linewidth = "0.5")

plt.show()
plt.savefig(filename + ".png", dpi=300)

Now i've another problem. I want to analyze this data, i.e. 50 minutes of data and divide it in subintervals, for example every 5 minutes with 10 total subintervals. I'm interested in the "rad" datas, the column near the hour in my original file. What i want to do is to get the average value for each subinterval (with the mean function), add the standard deviation error and plot it into a graph. I've a lot of file to analyze in that way and i would like to get an automized program.

All the help or external documentations would be appreciated. I'm pretty a newbie with python, thank you for all the help you'll give to me.

0

There are 0 answers