Change box width in grouped box plot

How can the width and spacing of a plotly box plot (e.g. px.box) be changed? Code example:

import pandas as pd
import plotly.express as px
df = pd.DataFrame({
    "val":  [ 1,  2,  1,  3,  1,  4,  3,  2,  2,  3,  3,  0],
    "cat1": ["A","A","A","A","B","B","B","B","C","C","C","C"],
    "cat2": ["X","X","Y","Y","X","X","Y","Y","X","X","Y","Y"]
})
fig=px.box(df,
           x="cat1",
           y="val", 
           color="cat2",
           boxmode="group")
fig.show()

Produces this nice plot:


It looks great, but let’s pretend I am unhappy with the widths of the boxrs and want to make them wider/smaller by using fig.update_traces(width=0.5). The result:

Suddenly, the boxmode="group" is ignored. Is this a bug? Or is there another way to change the width of box plots while still retaining grouping?

Based on this SO thread from 2014 I tried to set fig.update_layout(boxgroupgap=1) and fig.update_layout(boxgap=1), but neither made any difference here.

Version: plotly 4.12.0

Does that help you perhaps?

import pandas as pd
import plotly.express as px
df = pd.DataFrame({
    "val":  [ 1,  2,  1,  3,  1,  4,  3,  2,  2,  3,  3,  0],
    "cat1": ["A","A","A","A","B","B","B","B","C","C","C","C"],
    "cat2": ["X","X","Y","Y","X","X","Y","Y","X","X","Y","Y"]
})
fig=px.box(df,
           x="cat1",
           y="val", 
           color="cat2",
           boxmode="group")

fig.update_layout(height=600, width=300)

fig.show()

Thanks for the reply, but no. My goal is indeed to change the width of the boxes independent of the figure width. Combining box width and figure width does not help either:

import pandas as pd
import plotly.express as px
df = pd.DataFrame({
    "val":  [ 1,  2,  1,  3,  1,  4,  3,  2,  2,  3,  3,  0],
    "cat1": ["A","A","A","A","B","B","B","B","C","C","C","C"],
    "cat2": ["X","X","Y","Y","X","X","Y","Y","X","X","Y","Y"]
})
fig=px.box(df,
           x="cat1",
           y="val", 
           color="cat2",
           boxmode="group")
fig.update_traces(width=0.5)
fig.update_layout(height=300, width=600)
fig.show()

grafik

By the way, this works as expected for bar plots:

import pandas as pd
import plotly.express as px
df = pd.DataFrame({
    "val":  [ 1,  2,  1,  3,  1,  4,  3,  2,  2,  3,  3,  0],
    "cat1": ["A","A","A","A","B","B","B","B","C","C","C","C"],
    "cat2": ["X","X","Y","Y","X","X","Y","Y","X","X","Y","Y"]
})
fig=px.bar(df,
           x="cat1",
           y="val", 
           color="cat2",
           barmode="group")
fig.update_layout(height=300, width=600)
#fig.update_traces(width=0.2)
fig.show()

grafik
If I un-comment that fig.update_traces(width=0.2) line, Plotly gives me:
grafik
The bar widths are succesfully changed.
How can I achieve the same width change in a grouped box plot?

How about that:

import pandas as pd
import plotly.express as px
df = pd.DataFrame({
    "val":  [ 1,  2,  1,  3,  1,  4,  3,  2,  2,  3,  3,  0],
    "cat1": ["A","A","A","A","B","B","B","B","C","C","C","C"],
    "cat2": ["X","X","Y","Y","X","X","Y","Y","X","X","Y","Y"]
})
fig=px.box(df,
           x="cat1",
           y="val", 
           color="cat2",
           boxmode="group")
fig.update_layout(boxgroupgap=0.2, boxgap=0.8)
fig.show()

I get it now. I tried to change the width by fig.update_traces(width=0.5) and additionally set different values of boxgroupgap and boxgap in my tests. This does not work.

The trick seems to be to only use boxgroupgap and boxgap to indirectly set the width by specifiying the gaps. As soon as fig.update_traces(width=0.5) is added, the grouping is lost.

Thanks @windrose!

1 Like