如何在 Python 中使用 Plotly 绘制带有文本叠加的堆叠条形图?

How to plot Stacked Bar Chart with Text Overlay with Plotly in Python?

我正在尝试使用 Python 中的 Plotly 绘制 带有文本叠加层 的堆积条形图。喜欢下面的

示例数据

Fail_Word Fail_Count Pass_Word Pass_Count
properly 48 michigan 9
fraudulent 64 bodily 39
train 41 unauthorized 28
eos 42 insufficient 28
inaccurate 42 decision 8
strategy 41 program 18
escalate 14 inability 96
report 124 actuarial 128
register 14 account 86
applicable 42 annual 88

我试过下面的代码

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Bar(
    y=["Fail"],
    x=word_tr["Fail_Count"].to_list(),
    name="Fail",
    orientation='h',
    
))
fig.add_trace(go.Bar(
    y=["Pass"],
    x=word_tr["Pass_Count"].to_list(),
    name="Pass",
    orientation='h',
    
))

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

对于 Fail 栏,我想添加 Fail_WordFail_Count 作为堆叠栏,对于 Pass ---> Pass_WordPass_Count.

但无法生成所需的带文本叠加的堆叠条形图 图。有人可以阐明如何在 plotly 中绘制带有文本叠加的 堆积条形图 吗?

  • 主要是重组数据框,使其结构良好,适合 Plotly Express
index level_1 Count Word Percent
7 Fail 124 report 0.262712
1 Fail 64 fraudulent 0.135593
0 Fail 48 properly 0.101695
3 Fail 42 eos 0.0889831
4 Fail 42 inaccurate 0.0889831
  • 然后定义xycolortext
  • 终于更新texttemplate
  • 已排除小型贡献者,因为图中 space 不足以显示文本
import io
import pandas as pd
import plotly.express as px

df = pd.read_csv(
    io.StringIO(
        """Fail_Word,Fail_Count,Pass_Word,Pass_Count
properly,48,michigan,9
fraudulent,64,bodily,39
train,41,unauthorized,28
eos,42,insufficient,28
inaccurate,42,decision,8
strategy,41,program,18
escalate,14,inability,96
report,124,actuarial,128
register,14,account,86
applicable,42,annual,88"""
    )
)

# restructure dataframe for plotting
df2 = (
    pd.wide_to_long(
        df.reset_index(),
        stubnames=["Fail", "Pass"],
        i="index",
        j="data",
        sep="_",
        suffix="\w+",
    )
    .stack()
    .unstack(1)
    .reset_index()
    .sort_values(["level_1", "Count"], ascending=[1, 0])
    .groupby("level_1", as_index=False)
    .apply(lambda d: d.assign(Percent=d["Count"] / d["Count"].sum()))
)

fig = px.bar(
    df2.loc[df2["Percent"].gt(0.05)],
    y="level_1",
    x="Percent",
    color="level_1",
    orientation="h",
    text="Word",
)
fig.update_traces(texttemplate="%{text}<br>%{x:.1%}")
fig.update_layout(yaxis_title="", legend_title="", xaxis_tickformat=".0%")