时间点归入不同时间段分类,统计不同时间段数据量,matplotlib绘图排名,Python
https://zhangphil.blog.csdn.net/article/details/125912667
https://zhangphil.blog.csdn.net/article/details/125912667
把不同时间点(时刻)划入不同时间段,然后统计每个时间段的时间点(时刻)总量,最终把数据放入到pandas数据帧,然后用matplotlib对pandas数据绘图。
import datetime
import random
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
# 生成随机测试时间数量
from pprint import pprint
SAMPLE_COUNT = 2000
SECTION = 'section'
SUM = 'sum'
def my_time():
times = []
for i in range(24):
times.append({SECTION: (i, i + 1), SUM: 0})
cnt = 0
while cnt < SAMPLE_COUNT:
h = random.randint(0, 23)
m = random.randint(0, 59)
t = datetime.time(hour=h, minute=m)
for tx in times:
if tx[SECTION][0] <= t.hour < tx[SECTION][1]:
tx[SUM] = tx[SUM] + 1
# pprint(f'{t.strftime("%H:%M")} @ {tx[SECTION]}')
break
cnt = cnt + 1
return times
def drawchart(df):
myfont = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\msyh.ttc')
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
plt.rc('font', family='YaHei', weight='bold')
order = []
name = []
mem = []
for d, i in zip(df.values, df.index):
order.append(i)
name.append(d[0])
mem.append(int(d[1]))
FONT_SIZE = 12
fig, ax = plt.subplots(figsize=(15, 13))
b = ax.barh(y=range(len(name)), width=mem, align='center', color='red')
# 为横向水平的柱图右侧添加数据标签。
i = 0
for rect in b:
w = rect.get_width()
ax.text(x=w, y=rect.get_y() + rect.get_height() / 2, s='%d' % (int(w)),
horizontalalignment='left', verticalalignment='center',
fontproperties=myfont, fontsize=FONT_SIZE - 2, color='green')
ax.text(x=w / 2, y=rect.get_y() + rect.get_height() / 2, s=str(order[i]),
horizontalalignment='center', verticalalignment='center',
fontproperties=myfont, fontsize=FONT_SIZE - 3, color='white')
i = i + 1
ax.set_yticks(range(len(name)))
ax.set_yticklabels(name, fontsize=FONT_SIZE - 1, fontproperties=myfont)
ax.invert_yaxis()
ax.set_xlabel('数据', fontsize=FONT_SIZE + 2, fontproperties=myfont)
ax.set_title('不同时间段的数据点总量排名', fontsize=FONT_SIZE + 5, fontproperties=myfont)
# 不要横坐标上的label标签。
plt.xticks(())
# 清除四周的边框线
ax.get_yaxis().set_visible(True)
for spine in ["left", "top", "right", "bottom"]:
ax.spines[spine].set_visible(False)
plt.subplots_adjust(left=0.15) # 调整左侧边距
# ax.margins(y=0.01) #缩放 zoom in
ax.set_aspect('auto')
plt.show()
if __name__ == '__main__':
times = my_time()
# pprint(times)
# 数据组装成pandas数据帧。
pd_data = []
for t in times:
l = [t[SECTION], t[SUM]]
pd_data.append(l)
col = ['时间段', '时间点次数']
df = pd.DataFrame(data=pd_data, columns=col)
df = df.sort_values(by=col[1], axis=0, ascending=False) # 降序
# 重置索引
df = df.reset_index(drop=True)
df.index = df.index + 1
# 前10名
pprint(df.head(10))
# pprint(df.values)
drawchart(df)
输出:
时间段 时间点次数
1 (19, 20) 103
2 (21, 22) 99
3 (18, 19) 95
4 (3, 4) 90
5 (17, 18) 89
6 (1, 2) 88
7 (13, 14) 88
8 (12, 13) 86
9 (22, 23) 85
10 (16, 17) 83
绘制的图:
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://zhangphil.blog.csdn.net/article/details/125914326
内容来源于网络,如有侵权,请联系作者删除!