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
jvgomez
November 16, 2020, 11:54am
2
Bumping, I didnโt get any reply in 14 days
Hi! I have the same problems. Have you faound any solution?
jvgomez
February 12, 2021, 9:43am
4
No, I didnโt 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
print(fig) - find there corresponding โxaxisโ, โxaxis2โ
plotly.graph_objects.layout package โ 4.14.3 documentation - documentation for the axis
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')
)
Try to play with settings like tickmode, dtick, tick0
lpyeh
August 17, 2021, 7:16pm
6
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