↓ Suppose you want to plot the data of such a CSV file.
$ cat 1.csv "_time",count 1604847600,0 1604848200,2 1604848800,0 1604849400,2 1604850000,0 1604850600,3 1604851200,3 1604851800,0 1604852400,3 1604853000,1
1604847600 is a notation called UNIX TIME, which actually means as follows.
2020-11-09 00:00:00,0 2020-11-09 00:10:00,2 2020-11-09 00:20:00,0
Consider a program that converts this. .. ..
1from datetime import datetime 2 3import matplotlib.pyplot as plt 4import matplotlib.dates as md 5import dateutil 6 7import sys 8args = sys.argv 9 10x = [] 11y = [] 12 13f = open(args[1], 'r') 14 15line = f.readline() 16line = f.readline() 17 18while line: 19 tmp = line.split(",") 20 dt_object = datetime.fromtimestamp(int(tmp[0])) 21 22 x.append(str(dt_object)) 23 y.append(int(str(str(tmp[1]).replace('\n','')))) 24 25 line = f.readline() 26f.close() 27 28counter = 0 29for i in x: 30 print(x[counter] + "," + str(y[counter])) 31 counter = counter + 1
https://github.com/RuoAndo/qiita/blob/master/python-timeseries-plot/1.py
Make a list to store the time stamp and value on lines 10 and 11.
10 x = [] 11 y = []
Convert UNIX TIME to a date on line 20.
20 dt_object = datetime.fromtimestamp(int(tmp[0]))
Try to run it. .. ..
$ python 1.py 1.csv | more 2020-11-09 00:00:00,0 2020-11-09 00:10:00,2 2020-11-09 00:20:00,0 2020-11-09 00:30:00,2 2020-11-09 00:40:00,0 2020-11-09 00:50:00,3 2020-11-09 01:00:00,3 2020-11-09 01:10:00,0 2020-11-09 01:20:00,3 2020-11-09 01:30:00,1 2020-11-09 01:40:00,0 2020-11-09 01:50:00,1 2020-11-09 02:00:00,2 2020-11-09 02:10:00,2
(`) B
After that, make a program to plot this.
Take a look at the code. ..
1from datetime import datetime 2 3import matplotlib.pyplot as plt 4import matplotlib.dates as md 5import dateutil 6 7x = [] 8y = [] 9 10f = open('1.csv', 'r') 11 12line = f.readline() 13line = f.readline() 14 15while line: 16 tmp = line.split(",") 17 dt_object = datetime.fromtimestamp(int(tmp[0])) 18 19 x.append(str(dt_object)) 20 y.append(int(str(str(tmp[1]).replace('\n','')))) 21 22 line = f.readline() 23f.close() 24 25counter = 0 26for i in x: 27 print(x[counter] + "," + str(y[counter])) 28 counter = counter + 1 29 30dates = [dateutil.parser.parse(s) for s in x] 31 32plt.subplots_adjust(bottom=0.2) 33plt.xticks( rotation= 80 ) 34ax=plt.gca() 35xfmt = md.DateFormatter('%Y-%m-%d %H:%M') 36ax.xaxis.set_major_formatter(xfmt) 37plt.plot(dates,y) 38plt.show()
https://github.com/RuoAndo/qiita/blob/master/python-timeseries-plot/5.py
Plot the list x (timestamp), y (value) on lines 30-38.
30dates = [dateutil.parser.parse(s) for s in x] 31 32plt.subplots_adjust(bottom=0.2) 33plt.xticks( rotation= 80 ) 34ax=plt.gca() 35xfmt = md.DateFormatter('%Y-%m-%d %H:%M') 36ax.xaxis.set_major_formatter(xfmt) 37plt.plot(dates,y) 38plt.show()
(`) B
Recommended Posts