Black Lives Matter. Please consider donating to Black Girls Code today.

Vertical bar chart with a horizontal Line per bar

I’ve a vertical bar chart and would like to add horizontal lines indicating upper threshold values for specific bars (y axis is numeric obvisouly). Adding the lines to the plot can be done using shapes.

Here a Jupyterlab compatible FigureWidget code example:

import plotly.graph_objects as go

fig = go.FigureWidget()

fig = go.FigureWidget([go.Bar(x=['Bar 1', 'Bar 2', 'Bar 3'], y=[149.0, 350.3, 250.1])])

fig.update_layout(
    shapes=[
        go.layout.Shape(
            type="line",
            xref="paper",
            x0=0.035,
            y0=170,
            x1=0.3,
            y1=170,
            line=dict(
                width=4,
                dash="dash",
            ),
        ),
        go.layout.Shape(
            type="line",
            xref="paper",
            x0=0.365,
            y0=400,
            x1=0.635,
            y1=400,
            line=dict(
                width=4,
                dash="dash",
            ),
        ),
        go.layout.Shape(
            type="line",
            xref="paper",
            x0=0.7,
            y0=300,
            x1=0.965,
            y1=300,
            line=dict(
                width=4,
                dash="dash",
            ),
        ),
    ]
)

fig

However as the number of bars will change dynamically I could not align the line widths exactly to the bar widths yet (refer to x0 and x1values). What’s the best way to get this done?

Were you able to find a solution to this issue?