如何在 Python Plotly 堆叠条形图中显示多个文本和文本位置

How to show multiple text and text position in Python Plotly stacked bar chart

我在堆叠条中显示单个值。我还喜欢在顶部显示每个堆叠条的总数。可以吗?

我的代码:

df = pd.DataFrame({'Make':['Mercedes', 'BMW', 'Mercedes', 'Mercedes', 'Chrysler', 'Chrysler', 'Chrysler', 'Chrysler', 'BMW', 'Chrysler', 'BMW', 'Mercedes', 'BMW', 'Mercedes'],
                          'Dimension':['Styling', 'Styling', 'Price', 'Styling', 'MPG', 'MPG', 'Styling', 'Styling', 'MPG', 'MPG', 'Price', 'Price', 'Styling', 'MPG'],
                          'Country':['USA', 'USA', 'USA', 'Germany', 'USA', 'USA', 'USA', 'England', 'Germany', 'USA', 'Germany', 'Poland', 'Italy', 'USA'],
                          'LowValue':['64', '61', '70', '65', '59', '68', '63', '57', '58', '55', '69', '63', '69', '61']})
df_make = df.groupby(['Make','Dimension']).count()[['LowValue']].reset_index()

fig = px.bar(
    data_frame=df_make,
    x='Make',
    y="LowValue",
    color="LowValue",
    text="LowValue",
    
)
fig.update_traces(textposition='inside')

fig.show()

据我所知,没有办法直接执行此操作。但是你总是可以使用 fig.add_annotation() 和这样的设置:

for i,t in enumerate(totals):
    fig.add_annotation(x=x_uniq[i], y = t,
                       text = str(t),
                       showarrow = False,
                       yshift = 12,
                       font=dict(family="Courier New, monospace",
                                 size=18,
                                 color="firebrick"
                                )
                      )

其中 x_uniq 是图形 x 值的唯一出现 ['BMW', 'Chrysler', 'Mercedes'] ['BMW','BMW', 'BMW', 'Chrysler', 'Chrysler', 'Mercedes', 'Mercedes', 'Mercedes']totals 是图形 y 值的相应部分和 [1, 1, 2, 3, 2, 1, 2, 2] 映射到 x 值。下面的完整代码段包含有关如何获取这些值和生成此图的所有详细信息:

完整代码:

import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
import numpy as np
from itertools import groupby

df = pd.DataFrame({'Make':['Mercedes', 'BMW', 'Mercedes', 'Mercedes', 'Chrysler', 'Chrysler', 'Chrysler', 'Chrysler', 'BMW', 'Chrysler', 'BMW', 'Mercedes', 'BMW', 'Mercedes'],
                          'Dimension':['Styling', 'Styling', 'Price', 'Styling', 'MPG', 'MPG', 'Styling', 'Styling', 'MPG', 'MPG', 'Price', 'Price', 'Styling', 'MPG'],
                          'Country':['USA', 'USA', 'USA', 'Germany', 'USA', 'USA', 'USA', 'England', 'Germany', 'USA', 'Germany', 'Poland', 'Italy', 'USA'],
                          'LowValue':['64', '61', '70', '65', '59', '68', '63', '57', '58', '55', '69', '63', '69', '61']})
df_make = df.groupby(['Make','Dimension']).count()[['LowValue']].reset_index()

fig = px.bar(
    data_frame=df_make,
    x='Make',
    y="LowValue",
    color="LowValue",
    text="LowValue",
    
)
fig.update_traces(textposition='inside')

L = list(fig.data[0].x)
x_uniq, x_counts = np.unique(L, return_counts=True)
y_vals = fig.data[0].y
y_vals


elements = list(x_counts)

totals = []
index = 0
for i, e in enumerate(elements):
    if i == 0:
        totals.append(sum(y_vals[0:e]))
        index = index + e
    else:
        totals.append(sum(y_vals[index:index + e]))
        index = index + e
totals

for i,t in enumerate(totals):
    fig.add_annotation(x=x_uniq[i], y = t,
                       text = str(t),
                       showarrow = False,
                       yshift = 12,
                       font=dict(family="Courier New, monospace",
                                 size=18,
                                 color="firebrick"
                                )
                      )
fig.show()