任务是计算从1970年到今天(2018-02-05)每天发生的事件(在这种情况下,地震)的频率。 我有这个时间列表:
['1970-07-31T17:08:05.000Z', '1971-07-14T06:11:30.000Z',
'1971-07-26T01:23:22.000Z', '1972-12-02T00:19:52.000Z',
'1976-01-14T16:47:33.500Z', '1977-08-19T06:08:55.000Z',
'1985-03-03T22:47:07.280Z', '1985-09-19T13:17:47.350Z',
'1986-05-07T22:47:10.870Z', '1989-05-23T10:54:46.320Z',
'1994-06-09T00:33:16.230Z', '1994-10-04T13:22:55.840Z',
'1995-07-30T05:11:23.630Z', '1995-10-09T15:35:53.910Z',
'1996-02-17T05:59:30.550Z', '1998-03-25T03:12:25.070Z',
'2000-11-16T04:54:56.740Z', '2001-06-23T20:33:14.130Z',
'2003-09-25T19:50:06.360Z', '2004-12-23T14:59:04.410Z',
'2004-12-26T00:58:53.450Z', '2005-03-28T16:09:36.530Z',
'2006-05-03T15:26:40.290Z', '2006-11-15T11:14:13.570Z',
'2007-01-13T04:23:21.160Z', '2007-04-01T20:39:58.710Z',
'2007-08-15T23:40:57.890Z', '2007-09-12T11:10:26.830Z',
'2009-09-29T17:48:10.990Z', '2010-02-27T06:34:11.530Z',
'2011-03-11T05:46:24.120Z', '2012-04-11T08:38:36.720Z',
'2012-04-11T10:43:10.850Z', '2013-02-06T01:12:25.830Z',
'2013-05-24T05:44:48.980Z', '2014-04-01T23:46:47.260Z',
'2015-09-16T22:54:32.860Z', '2017-09-08T04:49:19.180Z']
从我有一个未完成的教程,这一步
In [114]: for i in range(1,len(time)):
dt = UTCDateTime(time[i])-UTCDateTime(time[i-1])
dt = dt / (3600*24)
inter_event_time.append(dt)
. . . . . :
In [115]: plt.hist(inter_event_time, bins=range(0,1000,100))
Out [115]:
(array([10, 7, 3, 5, 2, 0, 1, 2, 2]),
array([0, 100, 200, 300, 400, 500, 600, 700, 800, 900]),
<a list of 9 Patch objects>)
,最终结果是this(x轴是天,y轴是它发生的次数)。我的问题是我不知道inter_event_times应该是什么。帮帮我吧!
答案 0 :(得分:0)
如果您需要每天频率,这是一种方式。
import pandas as pd
from datetime import datetime
from collections import Counter
lst = ['1970-07-31T17:08:05.000Z', '1971-07-14T06:11:30.000Z',
'1971-07-26T01:23:22.000Z', '1972-12-02T00:19:52.000Z', etc.]
# convert to datetime & normalize, then count frequencies
counter_dict = Counter(pd.to_datetime(x).normalize().to_pydatetime() for x in lst)
# Counter({datetime.datetime(2012, 4, 11, 0, 0): 2,
# datetime.datetime(1970, 7, 31, 0, 0): 1, etc.})
为了产生直方图,你可以得到这样的范围的总和:
sum(counter_dict[k] for k, v in counter_dict.items() \
if (datetime(1970, 1, 1, 0, 0) < k < datetime(1979, 12, 31, 0, 0))) # 6
答案 1 :(得分:0)
我没有看过这些图片,但这有效:
import datetime
date_string = ['1970-07-31T17:08:05.000Z', '1971-07-14T06:11:30.000Z',
'1971-07-26T01:23:22.000Z', '1972-12-02T00:19:52.000Z',
'1976-01-14T16:47:33.500Z', '1977-08-19T06:08:55.000Z',
'1985-03-03T22:47:07.280Z', '1985-09-19T13:17:47.350Z',
'1986-05-07T22:47:10.870Z', '1989-05-23T10:54:46.320Z',
'1994-06-09T00:33:16.230Z', '1994-10-04T13:22:55.840Z',
'1995-07-30T05:11:23.630Z', '1995-10-09T15:35:53.910Z',
'1996-02-17T05:59:30.550Z', '1998-03-25T03:12:25.070Z',
'2000-11-16T04:54:56.740Z', '2001-06-23T20:33:14.130Z',
'2003-09-25T19:50:06.360Z', '2004-12-23T14:59:04.410Z',
'2004-12-26T00:58:53.450Z', '2005-03-28T16:09:36.530Z',
'2006-05-03T15:26:40.290Z', '2006-11-15T11:14:13.570Z',
'2007-01-13T04:23:21.160Z', '2007-04-01T20:39:58.710Z',
'2007-08-15T23:40:57.890Z', '2007-09-12T11:10:26.830Z',
'2009-09-29T17:48:10.990Z', '2010-02-27T06:34:11.530Z',
'2011-03-11T05:46:24.120Z', '2012-04-11T08:38:36.720Z',
'2012-04-11T10:43:10.850Z', '2013-02-06T01:12:25.830Z',
'2013-05-24T05:44:48.980Z', '2014-04-01T23:46:47.260Z',
'2015-09-16T22:54:32.860Z', '2017-09-08T04:49:19.180Z']
date_list = [datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ') for date in date_string]
date_start = datetime.datetime(1990, 1, 1)
date_end = datetime.datetime(2000, 1, 1)
count = 0
for date in date_list:
if date <= date_end and date >= date_start:
count += 1
print(count)
答案 2 :(得分:0)
要重新创建您提供的直方图,您需要添加.json
,如下所示:
inter_event_time = []