Search code examples
pythonplotlyplotly-python

How can I get rid of horizontal gaps between stacked bars when using go.Bar()?


This is an example from Plotly's documentation:

import plotly.graph_objects as go
fig = go.Figure(data=[
    go.Bar(name='SF Zoo', x=['giraffes', 'orangutans', 'monkeys'], y=[20, 14, 23]),
    go.Bar(name='LA Zoo', x=['giraffes', 'orangutans', 'monkeys'], y=[12, 18, 29])
])
fig.update_layout(barmode='stack')

On their website it looks like this: enter image description here

Here it looks like this: enter image description here

How can I get rid of the tiny gap in the monkeys column?


Solution

  • Those aren't gaps, but lines set with the color '#636efa'. You can ignore them by running:

    fig.update_traces(marker = dict(line = dict(color = 'rgba(0,0,0,0)')))
    

    Or just:

    fig.update_traces(marker_line_color =  'rgba(0,0,0,0)')
    

    Plot

    enter image description here

    Complete code:

    import plotly.graph_objects as go
    fig = go.Figure(data=[
        go.Bar(name='SF Zoo', x=['giraffes', 'orangutans', 'monkeys'], y=[20, 14, 23]),
        go.Bar(name='LA Zoo', x=['giraffes', 'orangutans', 'monkeys'], y=[12, 18, 29])
    ])
    
    fig.update_layout(barmode='stack')
    # fig.update_traces(marker = dict(line = dict(color = 'rgba(0,0,0,0)')))
    fig.update_traces(marker_line_color =  'rgba(0,0,0,0)')
    fig.show()