私はmatplotlibを使ってCSVファイルからインポートされたデータをプロットしています。Python datetimeで米国と英国の日付形式を切り替える
Date,Time,A,B
25/07/2016,13:04:31,5,25550
25/07/2016,13:05:01,0,25568
....
01/08/2016,19:06:43,0,68425
日付が、彼らは英国にあるであろうようにフォーマットされている、すなわち%d/%m/%Y
:これらのファイルは次の形式を持っています。最終的な結果は、2つのプロットを持つことです:A
が時間とともにどのように変化するか、そしてどのように変化するかB
時間の経過とともに変化します。
import matplotlib
matplotlib.use('Agg')
from matplotlib.mlab import csv2rec
import matplotlib.pyplot as plt
from datetime import datetime
import sys
...
def analyze_log(file, y):
data = csv2rec(open(file, 'rb'))
fig = plt.figure()
date_vec = [datetime.strptime(str(x), '%Y-%m-%d').date() for x in data['date']]
print date_vec[0]
print date_vec[len(date_vec)-1]
time_vec = [datetime.strptime(str(x), '%Y-%m-%d %X').time() for x in data['time']]
print time_vec[0]
print time_vec[len(time_vec)-1]
datetime_vec = [datetime.combine(d, t) for d, t in zip(date_vec, time_vec)]
print datetime_vec[0]
print datetime_vec[len(datetime_vec)-1]
y_vec = data[y]
plt.plot(datetime_vec, y_vec)
...
# formatters, axis headers, etc.
...
return plt
そして、すべてが8月1日前に罰金働いていた:私はそうのようなCSVからデータをインポートしています。しかし、それ以来、matplotlibは2016年1月8日(08 Jan)と私の01/08/2016データポイントをプロットしようとしています!
それは7月に1月からプロットしようとするため、私はプロットのエラーを取得:
RuntimeError: RRuleLocator estimated to generate 4879 ticks from 2016-01-08 09:11:00+00:00 to 2016-07-29 16:22:34+00:00:
はLocator.MAXTICKS * 2(2000)
を超えて、私はここで間違って何をしているのですか?上記のコードではprint文の結果は以下のとおりです。
2016-07-25
2016-01-08 #!!!!
13:04:31
19:06:43
2016-07-25 13:04:31
2016-01-08 19:06:43 #!!!!
..:関数は、構文解析に影響を与えるための2つのオプション、
dayfirst
はここに役立つはずがあり? – Phillip@Phillip '%d /%m /%Y'をフォーマットに入れたとき、' ValueError:time data '2016-07-25'が '%d /%m /%Y' ' – Frangipanes
@Frangipanes:あなたの入力は複数の形式を使用していますか? –