【问题标题】:histogram indexed by month and day [duplicate]按月和日索引的直方图[重复]
【发布时间】:2019-01-07 07:03:19
【问题描述】:

我正在尝试创建月/日对数的直方图。所以,我有一个包含以下内容的数组:

date_patterns = [(12,1,1992), (1,4,1993), (1,5,1993), 
                 (1,6,1993), (1,4,1994), (1,5,1994), 
                 (2,9,1995), (3,4,1995), (1,4,1996)]

我希望这个直方图只按月份和日期作为索引,所以:

(12,1) = 1
(1,4) = 3
(1,5) = 2
(1,6) = 1
(2,9) = 1
(3, 4) = 1

【问题讨论】:

  • 我想this is 你在找什么?
  • 不完全 - 我的数据在我想忽略的元组中有多年
  • 使用c=Counter([(a,b) for a,b,c in date_patterns]),其余显示在副本中。

标签: python matplotlib search histogram bar-chart


【解决方案1】:
import itertools
date_patterns = [(12,1,1992), (1,4,1993), (1,5,1993), 
                 (1,6,1993), (1,4,1994), (1,5,1994), 
                 (2,9,1995), (3,4,1995), (1,4,1996)]

#use a list comprehension to go through the date patterns grouped by day, month and then count the lengths of the groups
groups = [(k, len(list(g))) for k, g in  itertools.groupby(sorted(date_patterns), lambda x:(x[0], x[1]))]

print groups

【讨论】:

    猜你喜欢
    • 2014-10-09
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    相关资源
    最近更新 更多