Visitar URL original
[BUG]: barmode="group" not working with second y axis · Issue #5582 · plotly/plotly.py · GitHub
Skip to content

[BUG]: barmode="group" not working with second y axis #5582

Description

@inferrna
import plotly.graph_objects as go

def bar_chart_with_error_bars():
    # Trace C
    trace_c = go.Bar(
        x=["Trial 1", "Trial 2", "Trial 3"],
        y=[3, 6, 4],
        name="Trace C",
        error_y=dict(
            type="data",
            array=[1, 0.5, 1.5]
        )
    )

    # Trace E (secondary y-axis)
    trace_e = go.Bar(
        x=["Trial 1", "Trial 2", "Trial 3"],
        y=[400, 700, 300],
        name="Trace E",
        yaxis="y2",
        error_y=dict(
            type="data",
            array=[50, 100, 200]
        )
    )

    # Layout
    layout = go.Layout(
        barmode="group",
        yaxis=dict(title="Trace C"),
        yaxis2=dict(
            title="Trace E",
            overlaying="y",
            side="right"
        )
    )

    # Create figure
    fig = go.Figure(data=[trace_c, trace_e], layout=layout)

    # Save to HTML
    fig.write_html("/tmp/trials.html")


# Call the function
bar_chart_with_error_bars()

Resulting chart. Stack instead of group.
Image

rust version is also broken, but in different way - overlay instead of group: plotly/plotly.rs#391

Activity

  1. camdecoster commented on Apr 24, 2026

    @camdecoster
    Contributor

    Thank you for the report. Our team will take a look and follow up.

  2. emilykl commented on Jun 2, 2026

    @emilykl
    Contributor

    Hi @inferrna, thanks for the report, and apologies for the delayed follow-up. It seems that bar traces associated with different y-axes aren't automatically recognized as part of the same group, which I agree isn't the ideal behaviour.

    As a workaround, you can set the offsetgroup property of each trace to a different value, which will force Plotly to treat them as individual members of a bar group. For your code:

    import plotly.graph_objects as go
    
    def bar_chart_with_error_bars():
        # Trace C
        trace_c = go.Bar(
            x=["Trial 1", "Trial 2", "Trial 3"],
            y=[3, 6, 4],
            name="Trace C",
            error_y=dict(
                type="data",
                array=[1, 0.5, 1.5]
            ),
            offsetgroup=1,                                  #  added
    
        )
    
        # Trace E (secondary y-axis)
        trace_e = go.Bar(
            x=["Trial 1", "Trial 2", "Trial 3"],
            y=[400, 700, 300],
            name="Trace E",
            yaxis="y2",
            error_y=dict(
                type="data",
                array=[50, 100, 200]
            ),
            offsetgroup=2,                                  #  added
        )
    
        # Layout
        layout = go.Layout(
            barmode="group",
            yaxis=dict(title="Trace C"),
            yaxis2=dict(
                title="Trace E",
                overlaying="y",
                side="right"
            )
        )
    
        # Create figure
        fig = go.Figure(data=[trace_c, trace_e], layout=layout)
    
        # Save to HTML
        fig.write_html("/tmp/trials.html")
    
    
    # Call the function
    bar_chart_with_error_bars()

    which produces this result:

    Image
  3. removed their assignment
    on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions