これが私がそれを解決する方法です:
- csv ファイルを読み取ります。
csv
これは、Pythonのモジュールを使用して実行できます。
- ビンのサイズに応じて日付スタンプを読み取ったり変換したりして、各行を反復処理し、正しい時間ビンに追加します。私はそれを汚い方法で行い、分と秒を切り捨てました:
row[0][:-5]
return 15/07/2015 11
、作業する日付と時間。
status_records
最終的に、2 つのステータス オプションを表す 2 つの dict で構成されるリストが作成されます。これには時間ビンが含まれます。
"1" : {"15/07/2015 11": 3, ...}
"-11" : {"15/07/2015 11": 0, ...}
これは、いくつかのデータを含むサンプルdata.csv
です(実際に何かを見ることができるように、2つのエントリだけでは困難です-私は同じ日付形式とあなたが言及したステータスコードを使用しています):
start_time,exit_status
15/07/2015 11:53:47,1
15/07/2015 11:53:47,1
15/07/2015 11:54:56,1
15/07/2015 12:23:26,-11
15/07/2015 12:27:31,1
15/07/2015 14:01:47,-11
15/07/2015 14:11:56,1
15/07/2015 14:52:47,1
15/07/2015 15:53:23,1
15/07/2015 15:55:11,1
そして、ここに私のコードがあります(row[0]
csvを操作するには、対応する行に変更する必要があります):
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import csv
# 1. reading the csv
status_records = {'1': {}, '-11': {}}
with open('data.csv', 'rb') as csvfile:
reader = csv.reader(csvfile)
# 2. iterate through csv
for row in reader:
if row[0] == 'start_time': continue # first line
hour = row[0][:-5]
status = row[1]
# if hour not present, add empty 'slot' in each status bin
if hour not in status_records[status].keys():
status_records['1'][hour] = 0
status_records['-11'][hour] = 0
status_records[status][hour] = 1 # add the status we just read
else:
status_records[status][hour] += 1 # update status-hour bin
status1 = status_records['1'].values()
status2 = status_records['-11'].values()
print status1, status2
N = len(status1)
ind = np.arange(N)
width = 0.35
p1 = plt.bar(ind, status1, width, color='g')
p2 = plt.bar(ind, status2, width, color='r', bottom=status1)
plt.ylabel('# of exit status')
plt.title('Exit status in comparison with time')
plt.yticks(np.arange(0,11,10))
plt.legend((p1[0], p2[0]), ('1', '-11'))
plt.show()
出力:

改善: いくつかの便利なラベルを追加し、何も起こらない時間を表示するかどうかを決定することができます (ギャップでチャートが雑然とする可能性があります)。また、日付はそのままcsvでソートする必要があることに注意してください。それ以外の場合は、コードで自分でソートする必要があります。
とにかく、これで何かを始めることができます。