Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python plotly area chart with Y-axis in percent

Tags:

python

plotly

i try to plot the y-axis in percent. i there a way like in excel, excel scale automacily from 0 -100% ? or it is necessary first to calculate (group by year and continent) in the dataframe?


import plotly.express as px
df = px.data.gapminder()
fig = px.area(df, x="year", y="pop", color="continent",
          line_group="country")
fig.show()

thanks for helP!

like image 897
Alex Avatar asked Nov 21 '25 20:11

Alex


1 Answers

try this:

import plotly.express as px
df = px.data.gapminder()
dfw = df.groupby(['continent','year'])['pop'].sum().to_frame()
dfw.reset_index(inplace=True)

fig = px.area(dfw, x="year", y="pop", color="continent", groupnorm='fraction')
fig.update_layout(yaxis_tickformat='%') 
fig.show()

enter image description here

like image 178
r-beginners Avatar answered Nov 24 '25 09:11

r-beginners



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!