python - 将 pygal 值配置与 DateTimeLine 一起使用
问题描述
我正在绘制一些 CPU 时间统计数据,并希望能够注释某些值。我想为此使用pygal 的值配置,但这似乎无法与我正在使用的DateTimeLine图表结合使用。
def generate_cpu_time_plot(csv_file_path, output_file):
user = []
system = []
with open(csv_file_path, encoding="utf-8") as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
time = datetime.fromtimestamp(int(row['time_millis']) / 1000)
user.append((time, {
'value': float(row['cpu_time_user'])
}))
system.append((time, {
'value': float(row['cpu_time_system'])
}))
chart = pygal.DateTimeLine(x_label_rotation=35,
x_value_formatter=lambda dt: dt.strftime(
'%d/%m/%Y %H:%M'), x_title='Time',
y_title='CPU time',
title=os.path.basename(csv_file_path))
chart.add("User", user)
chart.add("System", system)
chart.render_to_file(output_file)
这给了我一个 TypeError:TypeError: '<' not supported between instances of 'dict' and 'dict'
有没有办法让这种组合发挥作用?如果我直接使用浮点数,没有字典,这很好。
解决方案
包含您提供给 XY 图表的 x 和 y 值的元组就是值。当您使用dict
提供值的格式时,您需要将value
属性设置为此元组。
目前,您的代码尝试将dict
y 值放入元组中。将值附加到user
和system
列表的行更改为以下内容应该可以修复它:
user.append({'value': (time, float(row['cpu_time_user']))})
system.append({'value': (time, float(row['cpu_time_system']))})
推荐阅读
- linux-kernel - jprobes 是否从内核 v4 中删除?
- python - 回归问题的 Hyperas 损失函数
- rust - Rust 中的隐式借用
- javascript - 怎么获得
来自 XMLHttpRequest 响应的标记值并在 html 中设置
- typescript - Typescript 中的动态枚举键
- python - Python 使用正则表达式捕获字符串中的特定模式
- highcharts - highcharts/highmap 中的零值
- java - 向一个方法发送数据,并一一检查
- javascript - iframe 跨域问题
- python - 使用多对多记录链接到创建时的现有记录