How to Draw Histograms in Gnuplot

Gnuplot has two histogram routes: for raw measurements, bin the values yourself with a small bin() function and smooth frequency; for data that is already counted per category, use the built-in histogram plotting style. The binning method below is the one you need most often.

The example uses values.dat — one measured value per line, no header. The goal is a bar per 0.5-wide value range showing how many measurements fall into it.

5.1
5.4
6.0
5.8
6.3
5.9
6.1
5.6
binwidth = 0.5
bin(x, width) = width * floor(x / width) + width / 2.0
set boxwidth binwidth * 0.9
set style fill solid 0.6 border -1
set title "Distribution of Values"
set xlabel "Value"
set ylabel "Frequency"
plot "values.dat" using (bin($1, binwidth)):(1.0) smooth frequency with boxes title "Measurements"

What each part does

The first two lines define the binning. binwidth is the width of each bar’s value range. The function bin(x, width) maps any value to the center of its bin: floor(x / width) finds which bin the value belongs to, and multiplying back plus half a width gives the bin center. Defining functions like this is a core gnuplot feature — user functions take arguments and return one value.

In the plot command, using (bin($1, binwidth)):(1.0) transforms each raw value (column 1, written $1 inside an expression) into its bin center, and pairs it with a count of 1.0. smooth frequency then sums those counts per distinct bin center, producing (bin center, total count) points. Finally with boxes draws a box at each point; set boxwidth binwidth * 0.9 makes the boxes slightly narrower than the bins so neighbouring bars stay visually separate, and set style fill solid 0.6 fills them at 60 percent opacity with a border.

Variants and going further

For pre-counted category data, use the dedicated style instead. Given a file with a label column and two count columns, set style data histograms plus set style histogram cluster gap 1 and then plot "groups.dat" using 2:xtic(1) title "Before", "" using 3 title "After" draws clustered bars with the labels from column 1 under each cluster — no binning needed because the counts are already in the file. Choose the method by the shape of your data: raw values → binning; a table of counts → histogram style.

Common mistakes

  • One giant spike or a single bar: the bin width does not match the data scale — a width of 0.5 on values in the thousands puts everything in a handful of bins. Print the data range with stats "values.dat" using 1 and pick a width that yields roughly 10–30 bins.
  • Boxes overlap into a solid block: set boxwidth was never set, so gnuplot used its default relative width. Set it from the same binwidth variable, slightly below 1.0 times the width.

Related tutorials: Boxplots in Gnuplot, Plot CSV Data in Gnuplot. See also the full Gnuplot Tutorials index.

Try it online: Paste a column of your own numbers as a datablock in the online terminal and adjust binwidth live to see the distribution change. 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 *