Search code examples
juliamakie.jl

Makie boxplot width is ignored if only one boxplot


I am using CairoMakie to plot a boxplot. The argument width in boxplot seems to work only if there are 2 or more boxplots to plot, but ignored if there is only one boxplot. For instance,

using CairoMakie

xs = rand(1:2, 1000)
ys = randn(1000)

boxplot(xs, ys; width=0.2)
current_figure()

correctly gives a slim boxplot look:

enter image description here

but doing this:

using CairoMakie

xs = rand(1:1, 1000)
ys = randn(1000)

boxplot(xs, ys; width=0.2)
current_figure()

instead gives a wide boxplot regardless what value I give to the width argument:

enter image description here

Is this a bug? Any workarounds so that plotting only 1 boxplot also gives me a slim boxplot. Thank you.


Solution

  • Both plots actually use the same width, in that the boxes take up the same x-axis range in both. You can see that the extent of the box is from 0.90 to 1.10 in the second plot - so it spans the 0.2 width that you've asked for.

    What's different is that, since the second plot only has one data point, the (automatically chosen) visual span of the x-axis is much smaller. At a glance, the first plot seems to be showing from x = 0.8 to 2.2, so the 0.2 width is relatively slim. The second plot is only showing from something like 0.89 to 1.11, so a 0.2 width is actually a big chunk of that.

    To make the box visually slim in the second plot, you can set the x-axis limits with xlims!:

    boxplot(xs, ys; width=0.2)
    xlims!(0, 2)
    current_figure()
    

    boxplot with x limits 0 to 2