0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python(matplotlib)でunixtimeの値の入った時系列データのCSVをプロットする

Posted at

↓このような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は,UNIXTIMEという表記法で、実際は次のような意味になる。


2020-11-09 00:00:00,0
2020-11-09 00:10:00,2
2020-11-09 00:20:00,0

これを変換するプログラムを考える。。。


     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

10,11行目でタイムスタンプと値を格納するリストを作る。


    10 x = []
    11 y = []

20行目でUNIXTIMEを日付に変換する。


    20    dt_object = datetime.fromtimestamp(int(tmp[0]))

実行してみる。。。


$ 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

あとはこれをプロットするプログラムを作る。

コードを見てみる。。


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

30行目から38行目でリストx(タイムスタンプ),y(値)をプロットする。


    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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?