Grouped boxplots in subplots have gaps in the X axis

Hi everyone!

I have an issue where boxplots grouped are displayed with an unexpected gap on the X axis when using subplots. Example:

Y1 = list(range(10))
Y2 = list(range(2,12))
X = [1]*5 + [2]*5
fig = make_subplots(rows=2, cols=1)
fig.add_trace(go.Box(x=X, y=Y1, marker_color ='red', name='Y1-1'), row=1, col=1)
fig.add_trace(go.Box(x=X, y=Y2, marker_color ='green', name='Y2-1'), row=1, col=1)

fig.add_trace(go.Box(x=X, y=Y1, marker_color ='blue', name='Y1-2'), row=2, col=1)
fig.add_trace(go.Box(x=X, y=Y2, marker_color ='yellow', name='Y2-2'), row=2, col=1)
fig.update_layout(boxmode='group')

iplot(fig)

Produces this output:

I was expecting to see the boxes at the same position along the X axis. How could I fix this?

Thank you!

NOTE: this issue has been reported already here, but it didn’t get any replies so I am trying with a new thread.

1 Like

Bumping, I didn’t get any reply in 14 days :frowning:

Hi! I have the same problems. Have you faound any solution?

No, I didn’t :frowning: And I am surprised that no developers replied.

In your case if the number of boxes and number of subplots won’t change I would

  1. print(fig) - find there corresponding ‘xaxis’, ‘xaxis2’
  2. plotly.graph_objects.layout package — 4.14.3 documentation - documentation for the axis
  3. You can update the axes with
fig.update_layout(

        xaxis=go.layout.XAxis(linecolor='black',
                              linewidth=1,
                              mirror=False,
                              showgrid=False,
                              zeroline=False,
                              title='Normalized log2 expression'),
      xaxis2=go.layout.XAxis(linecolor='black',
                              linewidth=1,
                              mirror=False,
                              showgrid=False,
                              zeroline=False,
                              title='Normalized log2 expression')
)
  1. Try to play with settings like tickmode, dtick, tick0

I’m having the same issue! I’m using shared_xaxes=True and boxmode='group', so all of my graphs shift more and more to the right as I add a subplot.

You should use offsetgroup argument of go.Box. For your example it looks like that:

Y1 = list(range(10))
Y2 = list(range(2,12))
X = [1]*5 + [2]*5
fig = make_subplots(rows=2, cols=1)
fig.add_trace(go.Box(x=X, y=Y1, marker_color ='red', name='Y1-1', offsetgroup='A'), row=1, col=1)
fig.add_trace(go.Box(x=X, y=Y2, marker_color ='green', name='Y2-1', offsetgroup='B'), row=1, col=1)

fig.add_trace(go.Box(x=X, y=Y1, marker_color ='blue', name='Y1-2', offsetgroup='A'), row=2, col=1)
fig.add_trace(go.Box(x=X, y=Y2, marker_color ='yellow', name='Y2-2', offsetgroup='B'), row=2, col=1)
fig.update_layout(boxmode='group')

fig.show()

It produces the following output:

1 Like