【问题标题】:TypeError histogram of timeTypeError 时间直方图
【发布时间】:2015-12-03 19:09:06
【问题描述】:

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

    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]

我的历史不正确。

编辑 2:

【问题讨论】:

  • 这和你之前的question有什么不同?
  • 这是原始代码,仅适用于小时数。我在这里指的是错误。我无法理解错误。 len(hour_list) 似乎工作正常。
  • 这里没有足够的数据来重现你的错误,发布原始输入数据和你的代码来制作你的df
  • 您的编辑不是编辑,而是答案。而且,实际上,这是下面@rurp 给出的答案(因此,即使您自己找到了答案,接受他的答案也是一个很好的姿态)。不要试图在对旧问题的编辑中提出新问题 - 它只会让发现这个问题的读者(例如来自谷歌)感到困惑
  • 我的建议是您从问题中提取两个直方图,并提出一个新问题,了解如何获得所需的 24 个 bin 的确切格式(例如 - 如果传递 bins=24 不起作用对你来说,为什么不呢?)

标签: python pandas matplotlib histogram


【解决方案1】:

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

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

【讨论】:

  • 谢谢!但请检查编辑我解决了这个问题。我有一个差异。不过问题。
  • 直方图显示每小时发生的次数。您希望它显示什么?
  • 那么应该有 24 个 bin 吧?
  • 您可以将所需的 bin 数量作为可选参数传入。 plt.hist(hour_list, bins=24)
  • 好问题,plt.hist(hour_list, bins=np.arange(24)-0.5) 会起作用的。这个链接有stackoverflow.com/questions/27083051/…
猜你喜欢
  • 2012-07-26
  • 2018-02-22
  • 2013-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多