简体   繁体   English

使用带有DateTimeLine的pygal值配置

[英]Using pygal Value configuration with DateTimeLine

I'm plotting some CPU time statistics and would like to be able to annotate certain values. 我正在绘制一些CPU时间统计信息,并希望能够注释某些值。 I wanted to use pygal's value configuration for this, but this does not seem to work in combination with the DateTimeLine chart I am using. 我想为此使用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)

This gives me a TypeError: TypeError: '<' not supported between instances of 'dict' and 'dict' 这给了我一个TypeError: TypeError: '<' not supported between instances of 'dict' and 'dict'

Is there a way I can make this combination work? 有没有办法让这个组合起作用? If I use the floats directly, without the dict, this works fine. 如果我直接使用浮动,没有字典,这工作正常。

The tuple containing the x and y values you provide to the XY chart is the value. 包含x和y的值,在提供到XY图表元组值。 When you use the dict format for providing values you need to set the value property to this tuple. 当您使用dict格式提供值时,您需要将value属性设置为此元组。

At the moment your code tries to put a dict for the y value within the tuple. 目前,您的代码尝试在元组中为y值放置一个dict Changing the lines where the values are appended to the user and system lists to the following should fix it: 将值附加到usersystem列表的行更改为以下内容应修复它:

user.append({'value': (time, float(row['cpu_time_user']))})
system.append({'value': (time, float(row['cpu_time_system']))})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM