在 plotly 中将计数百分比添加到堆积条形图

Adding percentage of count to a stacked bar chart in plotly

给定以下在 plotly 中创建的图表。

我想在每个块内添加 M 和 F 类别的每个计数的百分比值。

用于生成此图的代码。

arr = np.array([
        ['Dog', 'M'], ['Dog', 'M'], ['Dog', 'F'], ['Dog', 'F'],
        ['Cat', 'F'], ['Cat', 'F'], ['Cat', 'F'], ['Cat', 'M'],
        ['Fox', 'M'], ['Fox', 'M'], ['Fox', 'M'], ['Fox', 'F'],
        ['Dog', 'F'], ['Dog', 'F'], ['Cat', 'F'], ['Dog', 'M']
    ])

df = pd.DataFrame(arr, columns=['A', 'G'])

fig = px.histogram(df, x="A", color='G', barmode="stack")
fig.update_layout(height=400, width=800)

fig.show()

据我所知,Plotly 中的直方图没有文本属性。但是您可以自己生成条形图,然后通过文本属性添加百分比。

import numpy as np
import pandas as pd
import plotly.express as px

arr = np.array([
        ['Dog', 'M'], ['Dog', 'M'], ['Dog', 'F'], ['Dog', 'F'],
        ['Cat', 'F'], ['Cat', 'F'], ['Cat', 'F'], ['Cat', 'M'],
        ['Fox', 'M'], ['Fox', 'M'], ['Fox', 'M'], ['Fox', 'F'],
        ['Dog', 'F'], ['Dog', 'F'], ['Cat', 'F'], ['Dog', 'M']
    ])

df = pd.DataFrame(arr, columns=['A', 'G'])

df_g = df.groupby(['A', 'G']).size().reset_index()
df_g['percentage'] = df.groupby(['A', 'G']).size().groupby(level=0).apply(lambda x: 100 * x / float(x.sum())).values
df_g.columns = ['A', 'G', 'Counts', 'Percentage']

px.bar(df_g, x='A', y=['Counts'], color='G', text=df_g['Percentage'].apply(lambda x: '{0:1.2f}%'.format(x)))

请注意,您现在可以指定 plotly barnormtext_auto 参数来实现此目的。看看你的例子:

# Libraries
import numpy as np
import pandas as pd
import plotly.express as px

# Data
arr = np.array([
    ['Dog', 'M'], ['Dog', 'M'], ['Dog', 'F'], ['Dog', 'F'],
    ['Cat', 'F'], ['Cat', 'F'], ['Cat', 'F'], ['Cat', 'M'],
    ['Fox', 'M'], ['Fox', 'M'], ['Fox', 'M'], ['Fox', 'F'],
    ['Dog', 'F'], ['Dog', 'F'], ['Cat', 'F'], ['Dog', 'M']
])

df = pd.DataFrame(arr, columns=['A', 'G'])

#Plotly Code
fig = go.Figure()

fig = px.histogram (  df,
                      x="A",
                      color="G",
                      barnorm = "percent",
                      text_auto= True,
                      color_discrete_sequence=["mediumvioletred", "seagreen"],
                ) \
        .update_layout (

                    title={
                            "text": "Percent :A - G",
                            "x": 0.5
                          },

                    yaxis_title="Percent"
                ) \
        .update_xaxes(categoryorder='total descending')

fig.show()

一般来说,与自己计算百分比相比,它应该是首选的解决方案。这里输出: