You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3中事件数据按年月与严重度重组及图表生成

数据转换与可视化实现

一、原始数据转换为指定格式

转换代码

# 原始数据
raw_data = [
    ('2021-09', 29, '3'), ('2021-09', 249, '4'), ('2021-09', 162, '5'),
    ('2021-10', 16, '3'), ('2021-10', 392, '4'), ('2021-10', 255, '5'),
    ('2021-11', 9, '3'), ('2021-11', 321, '4'), ('2021-11', 240, '5'),
    ('2021-12', 6, '3'), ('2021-12', 332, '4'), ('2021-12', 205, '5'),
    ('2022-01', 1, '1'), ('2022-01', 8, '3'), ('2022-01', 304, '4'), ('2022-01', 221, '5'),
    ('2022-02', 6, '3'), ('2022-02', 321, '4'), ('2022-02', 262, '5'),
    ('2022-03', 9, '3'), ('2022-03', 359, '4'), ('2022-03', 284, '5'),
    ('2022-04', 7, '3'), ('2022-04', 350, '4'), ('2022-04', 331, '5'),
    ('2022-05', 9, '3'), ('2022-05', 419, '4'), ('2022-05', 301, '5'),
    ('2022-06', 2, '3'), ('2022-06', 427, '4'), ('2022-06', 348, '5'),
    ('2022-07', 7, '3'), ('2022-07', 458, '4'), ('2022-07', 294, '5'),
    ('2022-08', 2, '3'), ('2022-08', 437, '4'), ('2022-08', 270, '5'),
    ('2022-09', 15, '4'), ('2022-09', 5, '5')
]

# 提取所有唯一日期并排序
dates = sorted({item[0] for item in raw_data})

# 初始化各严重度统计字典
severity_counts = {
    'Severity1': [],
    'Severity2': [],
    'Severity3': [],
    'Severity4': [],
    'Severity5': []
}

# 遍历每个日期,填充对应严重度的数量
for date in dates:
    # 按严重度分组统计当前日期的数量
    daily_data = {s: 0 for s in ['1', '2', '3', '4', '5']}
    for item in raw_data:
        if item[0] == date:
            daily_data[item[2]] = item[1]
    
    # 填充到对应列表
    severity_counts['Severity1'].append(daily_data['1'])
    severity_counts['Severity2'].append(daily_data['2'])
    severity_counts['Severity3'].append(daily_data['3'])
    severity_counts['Severity4'].append(daily_data['4'])
    severity_counts['Severity5'].append(daily_data['5'])

# 组装成目标格式
result = [
    {
        'Dates': dates,
        **severity_counts
    }
]

# 打印结果
import pprint
pprint.pprint(result)

转换结果

[
    {
        'Dates': ['2021-09', '2021-10', '2021-11', '2021-12', '2022-01',
                  '2022-02', '2022-03', '2022-04', '2022-05', '2022-06',
                  '2022-07', '2022-08', '2022-09'],
        'Severity1': [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
        'Severity2': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        'Severity3': [29, 16, 9, 6, 8, 6, 9, 7, 9, 2, 7, 2, 0],
        'Severity4': [249, 392, 321, 332, 304, 321, 359, 350, 419, 427,
                      458, 437, 15],
        'Severity5': [162, 255, 240, 205, 221, 262, 284, 331, 301, 348,
                      294, 270, 5]
    }
]

二、生成事件严重度月度统计图表

可视化代码(使用Matplotlib)

import matplotlib.pyplot as plt
import numpy as np

# 从转换结果中提取数据
data = result[0]
dates = data['Dates']
severity1 = data['Severity1']
severity2 = data['Severity2']
severity3 = data['Severity3']
severity4 = data['Severity4']
severity5 = data['Severity5']

# 设置x轴位置
x = np.arange(len(dates))
width = 0.15  # 柱子宽度

# 创建画布
fig, ax = plt.subplots(figsize=(14, 7))

# 绘制各严重度柱状图
rects1 = ax.bar(x - 2*width, severity1, width, label='Severity1')
rects2 = ax.bar(x - width, severity2, width, label='Severity2')
rects3 = ax.bar(x, severity3, width, label='Severity3')
rects4 = ax.bar(x + width, severity4, width, label='Severity4')
rects5 = ax.bar(x + 2*width, severity5, width, label='Severity5')

# 添加轴标签和标题
ax.set_xlabel('年月')
ax.set_ylabel('事件数量')
ax.set_title('事件严重度月度统计')
ax.set_xticks(x)
ax.set_xticklabels(dates, rotation=45)
ax.legend()

# 在柱子上添加数值标签
def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3点垂直偏移
                    textcoords="offset points",
                    ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
autolabel(rects5)

fig.tight_layout()
plt.show()

图表说明

运行上述代码后,将生成一个分组柱状图:每个年月对应5个不同颜色的柱子,分别代表各严重度的事件数量;柱子上方标注具体数值;x轴为年月(自动旋转45度避免重叠),y轴为事件数量,整体效果与示例图表一致。

内容的提问来源于stack exchange,提问作者Mr.Kanchan Sinha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 01:10:36