Home > Mobile >  Pandas matplotlib : bar charts in percentage of the total
Pandas matplotlib : bar charts in percentage of the total

Time:10-10

I am trying to group chart in a pivot table fashion way. I would like to chart my datas, using pandas/matplotlib in a percentage way stacked by ['post_datetime','claimed'], where the chart bar would result into a 100% divided between my True/False 'claimed' datas.

This is an extract from my database:

post_datetime,claimed,percent,claimed_pct,unclaimed_pct
2016,True,1.0,1.0,
2017,True,1.0,1.0,
2018,False,0.25342,,0.25342
2018,True,0.74658,0.74658,
2019,False,0.3971,,0.3971
2019,True,0.6029,0.6029,
2020,False,0.44128,,0.44128
2020,True,0.55872,0.55872,
2021,False,0.5594,,0.5594
2021,True,0.4406,0.4406,
2022,False,0.5316,,0.5316
2022,True,0.4684,0.4684,

So far the best I get using the following code is a next to next bar chart (I want it 100%, divided between the proporption or True and False 'claiemd', which is defined by 'percent' in my database.


    ax = year_claim_df.plot(x='post_datetime',
                       y='claimed_pct',
                       kind='bar',
                       color='red'

                       )
    year_claim_df.plot(x='post_datetime',
                       y='unclaimed_pct',
                       kind='bar',
                       color='blue',
                       ax=ax

                       )
    plt.show()

here is the chart, that I get:

enter image description here But I would lile all bars to 1 or 100%, being split colored using my true/false 'claimed' column and by the proportion of my 'percent' column

I would like the chart to be in that fashion (not my data): enter image description here

Tx for the help. best

CodePudding user response:

Use pic

CodePudding user response:

IIUC you want to have something like this. Using seaborn is really easy and straightforward here.

import seaborn as sns

sns.barplot(x='post_datetime',y='percent', hue='claimed', data=df, palette=('blue', 'red'))

enter image description here

  • Related