首页 > 解决方案 > 如何在 matplotlib 中针对特定事件时间绘制类别

问题描述

假设您有 python 字典,它描述了在特定活动期间不同时间发生的“事件”。

activity = {
   "start_time":"2018-09-05 06:30:53.242",
   "end_time":"2018-09-09 09:30:53.242",
   "eventType1":[
      {
         "event_pos":123,
         "event_time":"2018-09-05 06:50:53.242",
         "message":"blah"
      },
      {
         "event_pos":125,
         "event_time":"2018-09-05 07:50:53.242",
         "message":"type1 event blah"
      }
   ],
   "eventType2":[
      {
         "event_pos":18,
         "event_time":"2018-09-05 06:40:58.242",
         "message":"type2 event1"
      },
      {
         "event_pos":700,
         "event_time":"2018-09-05 08:59:58.242",
         "message":"type2 event2"
      }
   ]
}

我的意图是在图表和/或表格中可视化这一点。最初我将此转换为一个表,前两行是开始和结束,事件实例、消息和时间作为后续行。

问题是事件可以重复,并且因为它们可以重复消息被重复(以毫秒为频率)。因此,后续事件列表可以非常迅速地增长,并且重复消息仅相隔几毫秒。这使得表格变得丑陋、难以阅读。

同时我所做的是通过消息获取事件的唯一实例,并显示每个事件消息的发生次数。这会清理一切,但这些事件发生的位置的视觉效果也很有用。

我的目标:

根据类别创建事件时间线。

我试过的:

关注这篇文章——> Matplotlib 时间线

但是我无法将类别(eventTypes)作为y轴,将python日期时间作为x轴。我转换为 matplotlib 日期时间字符串,但经常出现以下问题:

ValueError: x and y must have same first dimension, but have shapes (20,) and (2,)

问题:

  1. 当事件具有元数据(如消息和事件位置)时,如何在 matplotlib 中针对特定事件时间绘制类别
  2. 有没有更好的方法来可视化这种类型的数据?我愿意接受建议。

标签: pythonpandasmatplotlibdata-visualization

解决方案


推荐阅读