首页 > 解决方案 > 带有Y轴百分比的python plotly面积图

问题描述

我尝试以百分比绘制 y 轴。我有一种方法,比如在 excel 中,excel 自动从 0 -100% 缩放?还是有必要首先在数据框中计算(按年份和大陆分组)?


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

感谢帮助!

标签: pythonplotly

解决方案


尝试这个:

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()

在此处输入图像描述


推荐阅读