如何使用 plotly 从堆叠图表中绘制 100% 条形图?

how to plot 100% bar chart from a stacked chart with plotly?

您好,我正在尝试使用 plotly 在 streamlit 中使用 plotly 创建 100% 堆积条形图。我尝试使用 relative 但无济于事。

dfCategory = dfQuery.groupby(['l1_category_name','pricingPosition'])['pricingPosition'].count().reset_index(name="count")
fig = px.bar(dfCategory, x="count", y="l1_category_name", color='pricingPosition', orientation='h',
         height=400)
fig.update_layout(barmode='relative')
st.plotly_chart(fig, use_container_width=True)

如有任何帮助,我们将不胜感激

我觉得Plotly中的'relative'模式是一个总结正负的功能,而不是百分比显示。直方图函数具有百分比叠加功能,也可以用百分比显示。这是来自 official reference.

的示例
import plotly.express as px

long_df = px.data.medals_long()

fig = px.bar(long_df, x="nation", y="count", color="medal", title="Long-Form Input")

fig.update_layout(barmode='relative')
fig.show()

import plotly.express as px

long_df = px.data.medals_long()

fig = px.histogram(long_df, x="nation",
                   y="count", color="medal",
                   barnorm='percent', text_auto='.2f',
                   title="Long-Form Input")

fig.show()