Bar chart problem: alignment between bars and xlabels is offset

I have the problem that in my bar chart the bars and xlabels are not correctly aligned.

What do I have to change in my code to work on the alignment. I am not sure if it is the alignment of the bars or of the xlables.

def plot_model_parameters(df, title=""):
    max_parameters = 250000
    fig = px.bar(df, 
                x="architecture", 
                color= "architecture",  
                y='total_params',
                labels={"architecture": "Model Architectures",
                        "total_params" : "Parameters",
                            },
                #text_auto=".2s",
                title=f"Total Parameters {title}",
                barmode='group',
                height=height,
                template=template
                )

    fig.add_hline(y=max_parameters, line_width=2, line_dash="dash", line_color="blue", annotation_text=f"{max_parameters} Parameters", 
                annotation_position="top right")
    fig.update_traces(width=bar_width)
    fig.update_layout(width=width, height=height)
    return fig

fig = plot_model_parameters(basemodels, title=" - Phase 1: Base Models")
fig.update_layout(showlegend=False)
fig.show()

Thanks for your support.

Hi!

This example has text labels aligned with the bars. Iā€™d recommend using text = for your labels.

import plotly.express as px

df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
fig = px.bar(df, y='pop', x='country', text_auto='.2s',
            title="Default: various text sizes, positions and angles")
fig.update_traces(text = 'country')
fig.show()

I hope that helps,
Eliza

1 Like