星期-时间时刻散点平面坐标系统计大小量,Python,matplotlib(1)

zhangphil 2022-08-06 09:08:01 阅读数:686

时间平面星期时刻平面坐标

星期-时间时刻散点平面坐标系统计大小量,Python,matplotlib,散点图(1)

假设现在有一堆年月日时数据,形如t=2022-07-31 23:45:12,每一条这样的时刻都对应发生了一次事件event。反过来说,这个event在2D的平面坐标系可以分解为x坐标轴的时刻t=2022-07-31 23:45:12落入的区间(23,24),t=2022-07-31 23:45:12在y坐标轴分解落到星期日。因为t=2022-07-30 23:45:12这一个时间点,当天是星期日,具体的时间区间为(23,24),23点到24点。

如果有很多这样的时刻,那以为着某些点上会重复出现,比如x=(23,24),y=星期日,可能这样的点出现次数若干次为n,那么可以在平面坐标上把这些点圆形大小以n为直径绘制出来。

import datetime
from random import randrange
from datetime import timedelta
import matplotlib.pyplot as plt
from collections import Counter
from pprint import pprint
import numpy as np
SAMPLE_COUNT = 50
# 在start和end两个日期之间生成一个随机日期
def random_date(start, end):
delta = end - start
seconds_delta = (delta.days * 24 * 60 * 60) # 开始日期和结束日期之间相差的秒数
random_seconds = randrange(seconds_delta)
return start + timedelta(seconds=random_seconds)
def my_date_time():
date_times = []
start_date = datetime.datetime(2000, 1, 1)
end_date = datetime.datetime(2021, 12, 31)
cnt = 0
while cnt < SAMPLE_COUNT:
r = random_date(start_date, end_date)
date_times.append(r)
cnt = cnt + 1
return date_times
# 把数字0,1,2,3,4,5,6转换为星期*
# 0为星期一,6为星期日,依次类推
def number_to_weekday(number):
zh = ['一', '二', '三', '四', '五', '六', '日']
weekday = f'星期{zh[number]}'
return weekday
def date_to_points():
date_times = my_date_time()
hours = []
for i in range(24):
hours.append((i, i + 1))
points = []
for dt in date_times:
for h in hours:
if h[0] <= dt.hour < h[1]:
point = (h[0], dt.weekday(), h, dt) # weekday() # 0是星期一,6是星期日
points.append(point)
break
to_chart(points)
def to_chart(points):
print(points)
tx, ty = [], []
xy = []
for p in points:
tx.append(p[0])
ty.append(p[1])
xy.append((p[0], p[1]))
print(xy)
c_xy = Counter(xy)
pprint(c_xy)
sizes = []
for p in xy:
for k, v in c_xy.items():
if k == p:
print("坐标点{}, 出现{}次".format(k, v))
sizes.append(v * 50 + 10)
break
plt.style.use('_mpl-gallery')
# plot
plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文乱码
fig, ax = plt.subplots()
colors = np.random.uniform(15, 80, len(tx))
ax.scatter(tx, ty, s=sizes, c=colors, vmin=0, vmax=100)
x_labels = []
for i in range(24):
x_labels.append((i, i + 1))
y_labels = []
for i in range(7):
y_labels.append(number_to_weekday(i))
plt.xticks(ticks=np.arange(start=0, stop=24, step=1), labels=x_labels, rotation=45, size=10, color='red')
plt.yticks(ticks=np.linspace(start=0, stop=7, endpoint=False, num=7, dtype=np.int32), labels=y_labels, rotation=10)
plt.show()
if __name__ == '__main__':
date_to_points()

绘制的图:

输出:

[(15, 2, (15, 16), datetime.datetime(2020, 2, 5, 15, 46, 32)), (1, 0, (1, 2), datetime.datetime(2007, 3, 5, 1, 16, 30)), (14, 6, (14, 15), datetime.datetime(2017, 6, 11, 14, 21, 59)), (17, 1, (17, 18), datetime.datetime(2020, 4, 14, 17, 56, 26)), (3, 6, (3, 4), datetime.datetime(2012, 2, 19, 3, 36, 45)), (21, 3, (21, 22), datetime.datetime(2021, 6, 3, 21, 7, 45)), (17, 5, (17, 18), datetime.datetime(2006, 10, 14, 17, 25, 7)), (4, 0, (4, 5), datetime.datetime(2014, 10, 27, 4, 20, 42)), (21, 0, (21, 22), datetime.datetime(2014, 2, 24, 21, 5, 46)), (5, 3, (5, 6), datetime.datetime(2004, 2, 26, 5, 9, 18)), (14, 3, (14, 15), datetime.datetime(2002, 6, 20, 14, 20, 48)), (17, 1, (17, 18), datetime.datetime(2003, 11, 11, 17, 47, 49)), (19, 1, (19, 20), datetime.datetime(2003, 6, 3, 19, 11, 29)), (10, 2, (10, 11), datetime.datetime(2006, 1, 4, 10, 37, 58)), (23, 5, (23, 24), datetime.datetime(2007, 2, 17, 23, 21, 16)), (19, 6, (19, 20), datetime.datetime(2017, 11, 12, 19, 31, 41)), (21, 6, (21, 22), datetime.datetime(2016, 2, 21, 21, 53, 32)), (16, 0, (16, 17), datetime.datetime(2017, 5, 22, 16, 2, 4)), (5, 6, (5, 6), datetime.datetime(2018, 12, 2, 5, 34)), (7, 6, (7, 8), datetime.datetime(2009, 8, 23, 7, 51, 23)), (19, 6, (19, 20), datetime.datetime(2015, 1, 25, 19, 59, 26)), (1, 4, (1, 2), datetime.datetime(2016, 2, 26, 1, 49, 59)), (11, 1, (11, 12), datetime.datetime(2017, 8, 8, 11, 34, 17)), (9, 0, (9, 10), datetime.datetime(2013, 7, 22, 9, 30, 53)), (12, 6, (12, 13), datetime.datetime(2021, 11, 7, 12, 55, 9)), (12, 5, (12, 13), datetime.datetime(2009, 2, 7, 12, 47, 11)), (0, 2, (0, 1), datetime.datetime(2001, 12, 5, 0, 33, 45)), (13, 5, (13, 14), datetime.datetime(2015, 6, 13, 13, 40, 23)), (18, 1, (18, 19), datetime.datetime(2009, 1, 20, 18, 54, 6)), (20, 3, (20, 21), datetime.datetime(2012, 2, 9, 20, 23, 18)), (6, 3, (6, 7), datetime.datetime(2017, 2, 23, 6, 13, 26)), (2, 1, (2, 3), datetime.datetime(2005, 4, 19, 2, 6)), (10, 2, (10, 11), datetime.datetime(2014, 4, 30, 10, 15, 19)), (23, 0, (23, 24), datetime.datetime(2015, 3, 30, 23, 35, 29)), (19, 6, (19, 20), datetime.datetime(2011, 10, 30, 19, 20, 6)), (2, 1, (2, 3), datetime.datetime(2015, 1, 6, 2, 4, 21)), (7, 2, (7, 8), datetime.datetime(2020, 12, 16, 7, 15, 51)), (11, 1, (11, 12), datetime.datetime(2010, 11, 16, 11, 35, 1)), (9, 1, (9, 10), datetime.datetime(2003, 5, 6, 9, 23, 26)), (23, 2, (23, 24), datetime.datetime(2010, 6, 30, 23, 55, 23)), (11, 0, (11, 12), datetime.datetime(2005, 12, 26, 11, 38, 48)), (23, 4, (23, 24), datetime.datetime(2015, 7, 10, 23, 42, 25)), (3, 4, (3, 4), datetime.datetime(2000, 11, 17, 3, 51, 2)), (18, 2, (18, 19), datetime.datetime(2018, 1, 10, 18, 55, 23)), (21, 2, (21, 22), datetime.datetime(2005, 2, 2, 21, 56, 26)), (13, 3, (13, 14), datetime.datetime(2015, 1, 15, 13, 49, 5)), (23, 6, (23, 24), datetime.datetime(2015, 7, 19, 23, 2, 30)), (21, 2, (21, 22), datetime.datetime(2012, 1, 25, 21, 52, 40)), (6, 6, (6, 7), datetime.datetime(2016, 1, 10, 6, 37, 37)), (19, 0, (19, 20), datetime.datetime(2007, 5, 7, 19, 9, 37))]
[(15, 2), (1, 0), (14, 6), (17, 1), (3, 6), (21, 3), (17, 5), (4, 0), (21, 0), (5, 3), (14, 3), (17, 1), (19, 1), (10, 2), (23, 5), (19, 6), (21, 6), (16, 0), (5, 6), (7, 6), (19, 6), (1, 4), (11, 1), (9, 0), (12, 6), (12, 5), (0, 2), (13, 5), (18, 1), (20, 3), (6, 3), (2, 1), (10, 2), (23, 0), (19, 6), (2, 1), (7, 2), (11, 1), (9, 1), (23, 2), (11, 0), (23, 4), (3, 4), (18, 2), (21, 2), (13, 3), (23, 6), (21, 2), (6, 6), (19, 0)]
Counter({(19, 6): 3,
(17, 1): 2,
(10, 2): 2,
(11, 1): 2,
(2, 1): 2,
(21, 2): 2,
(15, 2): 1,
(1, 0): 1,
(14, 6): 1,
(3, 6): 1,
(21, 3): 1,
(17, 5): 1,
(4, 0): 1,
(21, 0): 1,
(5, 3): 1,
(14, 3): 1,
(19, 1): 1,
(23, 5): 1,
(21, 6): 1,
(16, 0): 1,
(5, 6): 1,
(7, 6): 1,
(1, 4): 1,
(9, 0): 1,
(12, 6): 1,
(12, 5): 1,
(0, 2): 1,
(13, 5): 1,
(18, 1): 1,
(20, 3): 1,
(6, 3): 1,
(23, 0): 1,
(7, 2): 1,
(9, 1): 1,
(23, 2): 1,
(11, 0): 1,
(23, 4): 1,
(3, 4): 1,
(18, 2): 1,
(13, 3): 1,
(23, 6): 1,
(6, 6): 1,
(19, 0): 1})
坐标点(15, 2), 出现1次
坐标点(1, 0), 出现1次
坐标点(14, 6), 出现1次
坐标点(17, 1), 出现2次
坐标点(3, 6), 出现1次
坐标点(21, 3), 出现1次
坐标点(17, 5), 出现1次
坐标点(4, 0), 出现1次
坐标点(21, 0), 出现1次
坐标点(5, 3), 出现1次
坐标点(14, 3), 出现1次
坐标点(17, 1), 出现2次
坐标点(19, 1), 出现1次
坐标点(10, 2), 出现2次
坐标点(23, 5), 出现1次
坐标点(19, 6), 出现3次
坐标点(21, 6), 出现1次
坐标点(16, 0), 出现1次
坐标点(5, 6), 出现1次
坐标点(7, 6), 出现1次
坐标点(19, 6), 出现3次
坐标点(1, 4), 出现1次
坐标点(11, 1), 出现2次
坐标点(9, 0), 出现1次
坐标点(12, 6), 出现1次
坐标点(12, 5), 出现1次
坐标点(0, 2), 出现1次
坐标点(13, 5), 出现1次
坐标点(18, 1), 出现1次
坐标点(20, 3), 出现1次
坐标点(6, 3), 出现1次
坐标点(2, 1), 出现2次
坐标点(10, 2), 出现2次
坐标点(23, 0), 出现1次
坐标点(19, 6), 出现3次
坐标点(2, 1), 出现2次
坐标点(7, 2), 出现1次
坐标点(11, 1), 出现2次
坐标点(9, 1), 出现1次
坐标点(23, 2), 出现1次
坐标点(11, 0), 出现1次
坐标点(23, 4), 出现1次
坐标点(3, 4), 出现1次
坐标点(18, 2), 出现1次
坐标点(21, 2), 出现2次
坐标点(13, 3), 出现1次
坐标点(23, 6), 出现1次
坐标点(21, 2), 出现2次
坐标点(6, 6), 出现1次
坐标点(19, 0), 出现1次

x=19,y=6,也即星期天的(19,20),发生的事件最多,圆最大。

版权声明:本文为[zhangphil]所创,转载请带上原文链接,感谢。 https://zhangphil.blog.csdn.net/article/details/126078731