↓ Supposons que vous souhaitiez tracer les données d'un tel fichier CSV.
$ 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 est une notation appelée UNIX TIME, ce qui signifie en fait ce qui suit.
2020-11-09 00:00:00,0 2020-11-09 00:10:00,2 2020-11-09 00:20:00,0
Considérez un programme qui convertit cela. .. ..
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
Faites une liste pour stocker les horodatages et les valeurs sur les lignes 10 et 11.
10 x = [] 11 y = []
Convertissez UNIX TIME en date à la ligne 20.
20 dt_object = datetime.fromtimestamp(int(tmp[0]))
Essayez de l'exécuter. .. ..
$ 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
Après cela, créez un programme pour tracer cela.
Jetez un œil au 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
Tracez la liste x (horodatage), y (valeur) de la 30e à la 38e ligne.
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