1
votes

I have a data frame data.

data

cast From        To 
A    12-01-1990  31-04-2000
B    01-02-1996  04-02-1997
C    03-09-1990  13-04-1994
D    09-09-1991  23-06-1999
E    09-01-1992  09-03-2000
F    01-02-1997  08-11-1999

Here, the data frame shows the availability of "cast" between the years.

How to plot a line graph(or any other) using matplotlib, seaborn or any other module in python between Year on x-axis and total number of cast available in the corresponding year on y-axis. For example, the number of cast available in year 1990 are 2; 1991 are 3; 1992 are 4; 1993 are 4; 1994 are 4; 1995 are 3; 1996 are 4; 1997 are 5; 1998 are 4; 1999 are 4 and finally 2000 are 2.

Plot with most scenic view is an added advantage. Thanks in advance.

1

1 Answers

1
votes

One way is to explode the columns into single years and count:

# min year and max year
min_year,max_year = df['From'].dt.year.min(), df['To'].dt.year.max()

# all years in data
year_df = pd.DataFrame({
    'active': range(min_year, max_year+1),
    'dummy':1
})

# merge, query for valid years and plot
(df.assign(dummy=1,
           From=df.From.dt.year,
           To=df.To.dt.year)
   .merge(year_df, on='dummy', how='left')
   .query('From <= active <= To')
   ['active'].value_counts(sort=False)
   .plot.bar()
)

Output:

enter image description here