Skip to content Skip to sidebar Skip to footer

Normalize Data And Plot As Stacked Bar Plot With Python/ Pandas

I have the following data frame: land_cover canopy_cat count tc_density_cor 0 20 1 56 35.760967 1 20 2 28 35.7609

Solution 1:

I think you need:

df['Count Per Canopy Cat'] = (df['count'] * df['tc_density_cor'] / 
                              df.groupby('land_cover')['count'].transform(sum))

df.pivot('land_cover',
         'canopy_cat',
         'Count Per Canopy Cat')\
  .plot.bar(stacked=True, figsize=(15,8))

Chart:

enter image description here


Solution 2:

IIUC

d = df.set_index(
    ['land_cover', 'canopy_cat']
).pipe(
    lambda d: d['count'].div(d['count'].sum(level=0), axis=0, level=0) * d['tc_density_cor']
).unstack()

d.iloc[:, :5]

canopy_cat          1         2         3         4         5
land_cover                                                   
20          15.768615  7.884308  3.097407  2.534242  1.126330
30           7.278454  3.226531  2.063479  1.350641  0.787874
40           7.908453  3.357362  1.417553  1.044513  0.671472

d.plot.bar(stacked=True)

enter image description here


Same answer refactored

def normalize(d):
    sums = d['count'].sum(level='land_cover')
    return d['count'].div(sums, axis=0, level='land_cover') * d['tc_density_cor']

d = df.set_index(['land_cover', 'canopy_cat']).pipe(normalize).unstack()

d.plot.bar(stacked=True)

Post a Comment for "Normalize Data And Plot As Stacked Bar Plot With Python/ Pandas"