Issue
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:
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):
Tx for the help. best
Solution
Use pivot
with rename
and then DataFrame.plot.bar
:
(df.pivot('post_datetime','claimed','percent')
.rename(columns={True:'claimed_pct',False:'unclaimed_pct'})
.plot.bar(stacked=True, color=['red','blue']))
Answered By - jezrael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.