带可变箱的堆叠条形图

2024-09-28 01:32:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有3个事件可能发生在同一年或不发生的时间序列。例如:

yr3 = [1950, 1954, 1955, ..]
yr2 = [1950, 1951, 1952, ..]
yr1 = [1951, 1953, 1957, ..]

同一年发生的那些事件,我想总结一下。仅在特定年份发生的事件应保持唯一性

一般来说,堆叠条形图的例子考虑同一个箱子中的事件。在这种情况下,它们可能在同一个箱子上,也可能不在。有办法解决吗

谢谢


Tags: 时间事件情况序列例子条形图年份办法
1条回答
网友
1楼 · 发布于 2024-09-28 01:32:14

在我看来,我会首先创建一本新词典,并使用年份作为“键”。 例如{1950:'yr3','yr2' , 1951:'yr1', ...}

如以下代码所示:

yr3=[1950,1954,1955,1956,1959]    
yr2=[1950,1951,1952,1956,1959]
yr1=[1951,1953,1957,1956,1960]
#Create a new dictionary
Year_Event_dict = dict()

yrs=[yr3,yr2,yr1]
events=['yr3','yr2','yr1']  
for i in range(len(yrs) ):
    for year in yrs[i] :
        if year not in Year_Event_dict.keys() :
            Year_Event_dict[year]=[events[i]]
        else :
            Year_Event_dict[year]+=[events[i]]

Year_Event_dict = sorted (Year_Event_dict.items() )
for year,events in Year_Event_dict :
    if len(events) > 1 :
        # not to print out the year when there was no or only one event happened.
        print ("in year %d, there were %d events happened, including  %s" %(year,len(events), events) )

它表明在某一年,事件的数量以及发生的事件类型。 你可以用这些信息来制作图表或其他东西。 希望这能对你有所帮助

相关问题 更多 >

    热门问题