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

How to get bars located to each other in bar plot with float x axis values?

I’ve a bar plot with float values as x axis values. I’d like to get the bars located close to each other what I get with

fig = go.Figure()
fig.add_trace(
    go.Bar(
        name='Class a',
        x=[10.1, 20.2, 30.3, 40.4],
        y=[1,2,3,4],
    )
)
fig.add_trace(
    go.Bar(
        name='Class b',
        x=[10.1, 20.2, 30.3, 40.4],
        y=[2,4,6,8],
    )
)

However my float values are seperated over a broader range which results in x axis interpreted as range somehow?

fig = go.Figure()
fig.add_trace(
    go.Bar(
        name='Class a',
        x=[61.1, 91.2, 199.3, 200.4],
        y=[1,2,3,4],
    )
)
fig.add_trace(
    go.Bar(
        name='Class b',
        x=[61.1, 91.2, 199.3, 200.4],
        y=[2,4,6,8],
    )
)

How can I get the bars close to each other (first picture) with the second case data?

Hi @fkromer, you can change the axis type from Cartesian to categorical for this

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(
    go.Bar(
        name='Class a',
        x=[61.1, 91.2, 199.3, 200.4],
        y=[1,2,3,4],
    )
)
fig.add_trace(
    go.Bar(
        name='Class b',
        x=[61.1, 91.2, 199.3, 200.4],
        y=[2,4,6,8],
    )
)
fig.update_layout(xaxis_type='category')
fig.show()

If you want to keep the right distances between xaxis points you can instead change the widths of bars as explained in
https://plot.ly/python/bar-charts/#customizing-individual-bar-widths.

@Emmanuelle Thx a lot for the hint with the bar widths.