vicemagui
vicemagui

Reputation: 151

Stacked bar plot with variable bins

I have 3 time series which events might occur in the same year or not. For instance:

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

Those events which occur in the same year I want to sum them. The ones which occur exclusively in a specific year should be maintained exclusive.

Generally, stacked bar plot examples consider events at the same bin. In this case, they might be on the same bin or not. Any idea to solve it?

Thanks.

Upvotes: 0

Views: 53

Answers (1)

HaoChien Hung
HaoChien Hung

Reputation: 113

In my opinions, I would first create a new dictionary and use year as "key". Such as {1950:'yr3','yr2' , 1951:'yr1', ...}

Like the following code:

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) )

It indicates that in certain year, the amount of events, and what types of events occurred. And you can use these information to make the graph or something else. Hope this could be helpful for you!

Upvotes: 1

Related Questions