【问题标题】:Text blocked by error bars on Plotly bar graphPlotly 条形图上的错误栏阻止的文本
【发布时间】:2021-10-29 05:11:57
【问题描述】:

我正在尝试制作带有误差线和数据标签的条形图,但我找不到如何更改文本的位置以使误差线不会干扰标签。 This is an example of what I currently have,代码如下:

x = [1, 2, 3, 4, 5, 6]
y = [100, 90, 80, 70, 60, 50]
y_err = [5, 6, 7, 8, 9, 10]

fig = go.Figure(data=go.Bar(x=x, y=y, error_y_array=y_err, text=y, textposition="inside"))
fig.show()

理想情况下,我希望标签位于栏的底部,但确保数据不被覆盖的任何其他方式也可以。我需要数据在栏内并水平定向。谢谢!

【问题讨论】:

    标签: python graph format plotly bar-chart


    【解决方案1】:

    我只是简单地注释每个条,textposition 的唯一有效参数是 `['inside'、'outside' 和 'auto']。因此,在这种情况下,遍历列表并添加如下文本:

    import plotly.graph_objects as go
    
    x = [1, 2, 3, 4, 5, 6]
    y = [100, 90, 80, 70, 60, 50]
    y_err = [5, 6, 7, 8, 9, 10]
    
    fig = go.Figure(data=go.Bar(x=x, y=y, error_y_array=y_err))
    
    i=0
    text_height = 5
    for x_val in x:
        fig.add_annotation(
            x=x_val,
            y=text_height, 
            text=str(y[i]),
            showarrow=False,
            font=(dict(color='white'))
        )
        i = i+1
    

    输出:

    【讨论】:

      猜你喜欢
      • 2018-12-07
      • 1970-01-01
      • 2020-09-28
      • 2021-12-28
      • 2016-02-25
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多