Python: Plotting percentage in seaborn bar plot

后端 未结 4 1048
离开以前
离开以前 2021-01-14 20:52

For a dataframe

import pandas as pd
df=pd.DataFrame({\'group\':list(\"AADABCBCCCD\"),\'Values\':[1,0,1,0,1,0,0,1,0,1,0]})

I am trying to pl

4条回答
  •  孤城傲影
    2021-01-14 21:19

    You can use the library Dexplot, which has the ability to return relative frequencies for categorical variables. It has a similar API to Seaborn. Pass the column you would like to get the relative frequency for to the count function. If you would like to subdivide this by another column, do so with the split parameter. The following returns raw counts.

    import dexplot as dxp
    dxp.count('group', data=df, split='Values')
    

    To get the relative frequencies, set the normalize parameter to the column you want to normalize over. Use True to normalize over the overall total count.

    dxp.count('group', data=df, split='Values', normalize='group')
    

    Normalizing over the 'Values' column would produce the following graph, where the total of all the '0' bars are 1.

    dxp.count('group', data=df, split='Values', normalize='Values')
    

提交回复
热议问题