How to Draw Boxplots in Gnuplot

A boxplot summarises a distribution as a box (the middle 50 percent of values) with whiskers and outlier points, and gnuplot computes it directly from raw data with set style boxplot. The script below compares the score distributions of two classes held in two columns of one file.

In scores.dat, column 1 is a row number, column 2 holds Class A scores and column 3 holds Class B scores, one student per row:

1  72  65
2  85  70
3  78  82
4  90  74
5  66  79
6  81  88
set style boxplot outliers pointtype 7
set style data boxplot
set boxwidth 0.6
set title "Scores by Class"
set ylabel "Score"
set xtics ("Class A" 1, "Class B" 2)
set xrange [0:3]
set grid y
plot "scores.dat" using (1):2 notitle, \
     "scores.dat" using (2):3 notitle
stats "scores.dat" using 2 name "A" nooutput
print sprintf("Class A: median %.1f, quartiles %.1f - %.1f", A_median, A_lo_quartile, A_up_quartile)

What each part does

set style data boxplot makes boxplot the default style for data plots, and set style boxplot outliers pointtype 7 says outliers (values beyond the whiskers) should be drawn, as filled circles. Each plot element becomes one box: using (1):2 places a box at x position 1 built from all values in column 2, and using (2):3 places the second box at x position 2 from column 3. The constant in parentheses is the box position — gnuplot needs an x coordinate per box, and a fixed number is the standard idiom.

Gnuplot computes the quartiles itself: the box spans the first to third quartile, the line inside marks the median, and the whiskers extend to the most extreme values within 1.5 times the interquartile range (that factor can be tuned with set style boxplot fraction). set xtics ("Class A" 1, "Class B" 2) replaces the numeric x labels with the class names. The trailing stats command computes the same quantities numerically into variables prefixed A_, and print sprintf(...) writes the median and quartiles to the terminal — handy when the text of a report needs the numbers, not just the picture. nooutput keeps stats from redrawing the plot.

Variants and going further

If each group lives in its own file, plot the files side by side instead: plot "a.dat" using (1):1, "b.dat" using (2):1 with the same boxplot settings. To sort boxes by median or place them by a category column from a long-format file, the standard approach is to split the file by group first (for example with every blocks or a small preprocessing step). Whisker behaviour can switch from the 1.5×IQR rule to fixed percentiles with set style boxplot fraction 0.95, which draws whiskers at the 2.5 and 97.5 percentiles.

Common mistakes

  • Boxes stacked at one x position: both plot elements used the same constant position. Give each box its own position value — (1), (2), (3) — and widen set xrange to match.
  • Boxplot style has no effect: the plot command overrode it with an explicit style such as with points, or set style data boxplot was set after set style boxplot in a way that reset options. Keep the two set style lines together before the plot and add no with clause to the box elements.

Related tutorials: Histograms in Gnuplot, Curve Fitting with fit. See also the full Gnuplot Tutorials index.

Try it online: The boxplot quartiles are computed from the data you paste in, so the online terminal is a quick stats check for a small column of numbers. Paste the script into the Gnuplot Online terminal and run it in your browser — no install needed. To run gnuplot locally instead, see the installation guide and the download page.

Go deeper: For a systematic walk through gnuplot from first plots to publication-ready figures, see Gnuplot in Action by Philipp K. Janert — Janert is a gnuplot developer, and the book is the most complete tutorial reference for the program. Affiliate link: see our Affiliate Disclosure.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *