Plotly - 在文本标注不可见时添加文本轮廓或更改颜色?

Plotly - Add text outline or change color when text callout not visible?

我有以下代码在 Plotly 中创建此图表

fig = go.Figure()
fig = make_subplots(specs=[[{"secondary_y": True}]])

fig.add_trace(
    go.Bar(
        name         = "Registrazioni - Bank",
        marker_color = '#5B27A1', 
        x            = bnk_wk_cpa['Week'].dt.strftime('%d %b'),
        y            = bnk_wk_cpa['Registrazione - Bank'],
        text         = round(bnk_wk_cpa['Registrazione - Bank'],0),
        hoverinfo    = 'text',
        textposition = 'inside',
        width        = 0.65,
                            ),
     secondary_y  = False
)

fig.add_trace(
    go.Scatter(
        name         = 'CPA',
        marker_color = '#FD009D', 
        x            = bnk_wk_cpa['Week'].dt.strftime('%d %b'),
        y            = bnk_wk_cpa['CPA - Bank'],
        text         = round(bnk_wk_cpa['CPA - Bank'],2),
        line         = dict(
#        shape        = 'spline',
        width        = 3.5),
        mode         = 'lines+markers+text',
        hoverinfo    = 'text',
        textposition = 'top center'),
    secondary_y  = True
)


fig.add_trace(
    go.Bar(
        name         = "Spend",
        marker_color = '#321461', 
        x            = bnk_wk_cpa['Week'].dt.strftime('%d %b'),
        y            = np.full(len(bnk_wk_cpa), 1),
        text         = round(bnk_wk_cpa['Spend - Bank'],1),
        hoverinfo    = 'text',
        textposition = 'inside',
        textfont     = dict(
        size         = 25),
        width        = 0.65,
        yaxis        = "y3",
                            ),
)


fig.update_traces(texttemplate='%{text:.2s}')

fig.update_layout(
    showlegend          = True,
    paper_bgcolor       = "rgba(0,0,0,0)",
    plot_bgcolor        = "rgba(0,0,0,0)",
    margin              = dict(
    t                   = 30,
   # l                   = 10,
   # b                   = 10,
   # r                   = 10
    ),
    font_color          = '#000',
    font_family         = 'Trebuchet MS',
    font_size           = 24,
    width               = 1633, 
    height              = 750,
    legend              = dict(
    yanchor             = "top",
    y                   = 1.13,
    xanchor             = "left",
    x                   = 0.015,
    title               = '',
    orientation         = 'h',
    ))


fig.update_yaxes(visible=False)

fig.update_layout(
    yaxis={
        "domain": [0, 0.88],
    },
    yaxis2={
        "domain": [0, 0.88],
    },
    yaxis3={"domain": [0.92, 0.98], "visible": False},
)

fig.update_traces(hovertemplate=None)
fig.update_layout(hovermode="x unified")

fig.show()

我想让 CPA 行的标注文本始终可见。我想知道是否可以:

A.文字进入条形图时改变颜色

B. 在文本周围添加一个白色文本描边,使其始终可见(见下图,使用 Google 表格完成)

C. 也许在两个图表之间绘制折线图,​​以便标注数据始终可见 - 这意味着我想象的折线图的高度更小?

因为您没有添加数据来重新创建图表,我会给您选择如何执行此操作

对于一个 您可以使用 go.Scatter 函数

中的 textfont 参数控制文本颜色

你有2个选项 1- 为所有文本设置 1 种颜色,操作如下

fig.add_trace(
    go.Scatter(
        name         = 'CPA',
        marker_color = '#FD009D', 
        x            = bnk_wk_cpa['Week'].dt.strftime('%d %b'),
        y            = bnk_wk_cpa['CPA - Bank'],
        text         = round(bnk_wk_cpa['CPA - Bank'],2),
        line         = dict(
#        shape        = 'spline',
        width        = 3.5),
        mode         = 'lines+markers+text',
        hoverinfo    = 'text',
        textfont=dict(
            color="magenta",
            
    )
        textposition = 'top center'),
    secondary_y  = True
)

或者您可以传递一个基于位置的颜色值列表

 textfont={
        "color": ["MidnightBlue", "IndianRed", "MediumPurple", "Gold", "Crimson",
                  "LightSeaGreen",
                  "RoyalBlue", "LightSalmon", "DarkOrange", "MediumSlateBlue"],
        "family": ["Arial, sans-serif", "Balto, sans-serif", "Courier New, monospace",
                   "Droid Sans, sans-serif", "Droid Serif, serif",
                   "Droid Sans Mono, sans-serif",
                   "Gravitas One, cursive", "Old Standard TT, serif",
                   "Open Sans, sans-serif",
                   "PT Sans Narrow, sans-serif", "Raleway, sans-serif",
                   "Times New Roman, Times, serif"],
        "size": [22, 21, 20, 19, 18, 17, 16, 15, 14, 13]
    }

对于边框,您需要将它们添加为注释

在此处查看情节示例https://plotly.com/python/text-and-annotations/