Skip to content

Box Plot

When to use

Box plots are ideal for:

  • Comparing distributions between multiple groups.
  • Identifying median, spread, and outliers at a glance.
  • Scientific experiments, A/B tests, and benchmarking.

Use ax.Box(groups, chart.BoxStyle{...}) where groups is a slice of value slices — one per group.


Basic example

package main

import (
	"github.com/goplotlib/goplotlib/chart"
	"github.com/goplotlib/goplotlib/plot"
)

func main() {
	groups := [][]float64{
		{12, 15, 14, 10, 18, 22, 9, 16, 13, 20},
		{25, 28, 22, 30, 35, 20, 27, 32, 24, 29},
		{8,  11,  9, 14,  7, 12, 10, 15,  6, 13},
	}

	fig := plot.New(plot.WithWidth(860), plot.WithHeight(420))
	ax := fig.AddAxes()
	ax.Box(groups, chart.BoxStyle{
		Labels: []string{"Control", "Treatment A", "Treatment B"},
	})
	ax.SetTitle("Experimental Results").SetYLabel("Score")
}

Box plot


Visual elements

Each box encodes the following statistics computed using linear interpolation:

ElementValue
Box bottomQ1 (25th percentile)
White centre lineMedian (Q2)
Box topQ3 (75th percentile)
Whisker endsMost extreme value within Q1 − 1.5 × IQR and Q3 + 1.5 × IQR
CirclesOutliers outside the 1.5 × IQR fences

Style reference

chart.BoxStyle fields:

FieldTypeDefaultDescription
Labelstring""Legend label for the series
Colorcolor.ColorpaletteOverride the automatic palette color
Labels[]stringindexPer-group x-axis category labels