Hi everyone,
Is there any direct way to produce a percentage stacked bar chart with Plotly Express?
I have the following dataframe
candidato Page Name Likes
0 AndrĂ© Ventura AgĂȘncia Lusa 95
1 André Ventura CM TV 2120
2 André Ventura CNN Portugal 13825
3 André Ventura Correio da Manhã 5579
4 AndrĂ© Ventura DiĂĄrio de NotĂcias 1381
5 André Ventura ECO 101
6 André Ventura Expresso 4070
7 André Ventura Jornal Económico 665
8 André Ventura Jornal de Negócios 1355
The dataframe repeats for 8 more candidatos
and has 22 Page Names
The result Iâm trying to achieve is this
The reason for this is that a regular stacked bar chart fails to give the dimensions between candidates, as you can see in this example.
Is there any way to do this?
Thank you in advance!
Disclosure: This is part of a media monitor report that Iâm voluntarily preparing, The final product will be posted under a creative commons license, and there is no commercial intent.
1 Like
Hi @jgomes_eu,
To my knowledge there isnât a direct way to do it, but it is very straightforward to transform the data in pandas. See for instance this SO post.
Hi, thank @jlfsjunior
The solution presented really doesnât work for me, as my dataset is a bit more complex than that.
Totals have to be calculated for each Page Name
for each of column (Total Interactions,Likes,Shares,Love,Angry), and then percentages calculated.
Is this something that is on the roadmap for Plotly.Express?
It would be really useful as this is a type of chart that is commonly used for data visualization.
Assuming your dataset resides in a dataframe named data
, the following commands will create the graph you are looking for:
df = data.groupby([âPage Nameâ, âcandidatoâ]).agg({âLikesâ: âcountâ})
df[âpropâ] = df.groupby(level=0).apply(lambda x: 100*x/x.sum()).reset_index(level=0, drop=True)
fig = px.bar(df.reset_index(), x=âPage Nameâ, y=âpropâ, color=âcandidatoâ)
fig.show()
I hope it helps, even though it is a bit too late.
1 Like