python - 时间类型误差直方图

标签 python pandas matplotlib histogram

我正在尝试按小时绘制时间

    time_new=[x[:2]+":"+x[2:] for x in time_cleaned]        
    hour_list = [t[:2] for t in time_new]
    print hour_list
    numbers=[x for x in xrange(0,24)]
    labels=map(lambda x: str(x), numbers)
    plt.xticks(numbers, labels)
    plt.xlim(0,24)
    pdb.set_trace()
    plt.hist(hour_list)
    plt.show()

我在 plt.hist(hour_list) 行中收到此错误 TypeError: 'len() of unsized object'

pprint(time_new)
['09:00',
 '23:30',
 '19:05',
 '09:00',
 '01:00',
 '02:00',
 '19:00',
 '05:30',
 '04:00',
 '20:00',
 '23:30',
 '10:30',
 '20:00',
 '05:0',
 '21:30',
 '17:30',
 '04:55',
 '13:45',
 '08:40',
 '13:00',
 '06:00',
 '19:45',
 '09:00',
 '14:30',
 '09:00',
 '10:30',
 '23:07',
 '19:00',
 '23:40',
 '20:30',
 '19:30',
 '06:00',
 '05:30',
 '24:00',
 '20:30',
 '19:00',
 '15:05',
 '14:15',
 '19:20',
 '14:00',
 '15:15',
 '21:00']
(Pdb) 

编辑: 修复它:

hour_list = [int(t[:2]) for t in time_new]

我的历史不正确。 enter image description here

编辑2: enter image description here

最佳答案

看起来您正在尝试将字符串绘制为值。尝试将 hour_list 更改为:

hour_list = [int(t[:2]) for t in time_new]

关于python - 时间类型误差直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32452027/

相关文章:

python - 如何将数据帧转换为所需的格式?

python - Pandas 版本的“如果为真,则在此处进行 VLOOKUP,如果为假,则在其他地方进行 VLOOKUP

python - Pandas:有没有办法使用类似 'droplevel' 的东西,并在处理过程中使用丢弃的级别标签作为前缀/后缀重命名其他级别?

python - 为什么 matplotlib 子图以 1 开头

python - Pandas DataFrame.hist() 不起作用

python - 根据多列之间的差异过滤数据框

python - 查找键/将多个值附加到字典中的键时出现问题(python)

python - 增加 matplotlib 注释文本中的行间距

python - 在 Python 中打印简单的菱形图案

python - 在 CPU 上运行 tf.slim 评估循环