pythonplotly - 在跟踪之外显示文本不起作用

问题描述 投票:0回答:1

我正在尝试在图表上显示底部(深橙色)轨迹之外的百分比。

这是我的代码:

bar2 = go.Figure()
bar2.add_trace(go.Bar(name = 'Halted Trials', 
                     x = bar2_df['Condition'],
                     y = bar2_df['Halted'],
                      text = bar2_df['Percent'],
                      texttemplate = "%{text:.2f}%<br>",
                      textposition = 'outside',
                     marker = dict(color = 'rgb(252, 141, 98)'),
                    ))
bar2.add_trace(go.Bar(name = 'Total Trials', 
                     x = bar2_df['Condition'],
                     y = bar2_df['Total'],
                     marker = dict(color = 'rgb(252, 200, 179)'),
                    ))

bar2.update_layout(barmode = 'stack',
                  yaxis_title = 'Clinical Trials',
                  width = 1000,
                  height = 1000,
                  margin = dict(l = 0, r = 0, t = 0, b = 0, pad = 0),                  
                  legend = dict(
                      orientation = 'h',
                      yanchor = 'bottom',
                      y = -.3,
                      xanchor = 'center',
                      x= .5),
                  plot_bgcolor = 'white',
                  )

下面是上面代码生成的图表: enter image description here

我尝试设置 textposition = 'outside',但将其设置为 'inside' 或 'outside' 显示相同的结果 - 文本位于迹线的内部。我有什么遗漏的吗?

更新: 他们的文档解释了为什么会发生这种情况。

文本位置 代码:fig.update_traces(textposition=,selector=dict(type='bar')) 类型:枚举或枚举数组,其中之一 ( "inside" | "outside" | "auto" | "none" ) 默认值:“自动” 指定

text
的位置。 “内部”位置
text
内部,靠近条形末端(如果需要,可旋转和缩放)。 “outside”位于
text
外部,靠近条形末端(如果需要可缩放),除非在该条形上堆叠有另一个条形,否则文本将被推入内部。 “auto”尝试将
text
定位在条形内部,但如果条形太小且没有条形堆叠在该条形上,则文本将移至外部。如果“无”,则不显示任何文本。

我的下一个问题——如何修改代码来创建我想要的输出?

python python-3.x plotly plotly-python plotly.graph-objects
1个回答
0
投票

我最终通过参考这个堆栈溢出帖子解决了这个问题,根据每个条形的单独 y 值创建注释。

这是我的结果代码:

halt_cond = pd.DataFrame()
halted_counts = halt_df.groupby('Condition').size().reset_index(name = 'Halted')
total_counts = clin_trials.groupby('Condition').size().reset_index(name = 'Total')
halt_cond = pd.merge(halted_counts, total_counts, on='Condition', how = 'outer')
halt_cond.fillna(0, inplace=True)
halt_cond['Percent'] = (halt_cond['Halted']/halt_cond['Total']).map('{:.2%}'.format)

# create the dataframe of top 10 conditions
bar2_df = halt_cond.sort_values(by = 'Total', ascending = False).head(10)

# create the list for text annotations
xi = bar2_df['Condition']
yi = bar2_df['Halted']
annoText = bar2_df['Percent']
annotationsDict = [dict(
    x = xcoord,
    y = ycoord + 20,
    text = annoText,
    showarrow = False,
) for xcoord, ycoord, annoText in zip(xi, yi, annoText)]


bar2 = go.Figure()
bar2.add_trace(go.Bar(name = 'Halted Trials', 
                     x = bar2_df['Condition'],
                     y = bar2_df['Halted'],
                     marker = dict(color = 'rgb(252, 141, 98)'),
                    ))
bar2.add_trace(go.Bar(name = 'Total Trials', 
                     x = bar2_df['Condition'],
                     y = bar2_df['Total'],
                     marker = dict(color = 'rgb(252, 200, 179)'),
                    ))

bar2.update_layout(barmode = 'stack',
                  yaxis_title = 'Clinical Trials',
                  width = 1000,
                  height = 1000,
                  margin = dict(l = 0, r = 0, t = 0, b = 0, pad = 0),
                  legend = dict(
                      orientation = 'h',
                      yanchor = 'bottom',
                      y = -.3,
                      xanchor = 'center',
                      x= .5),
                   annotations = annotationsDict,
                   plot_bgcolor = 'white'
                  )
© www.soinside.com 2019 - 2024. All rights reserved.