Creating histograms in gnuplot

I needed to produce a histogram in gnuplot of multiple sets of raw data (i.e. not sumarised). The data in each dataset required binning and displaying on the same plot. This can be achieved by first defining a binning function which identifies which bin a given data point should be placed in:


The plot style is set up as preferred, in this case using solid filled boxes for the histogram with no border.

set style fill solid border -1
set boxwidth 0.5*binwidth

Finally, we plot the data using the smooth freq option which oversees the binning of the histogram data.

plot 'data1.dat' using (bin($3,binwidth)):(1.0) smooth freq with boxes,
     'data2.dat' using (0.5*binwidth+bin($3,binwidth)):(1.0) smooth freq with boxes

Here we have defined a variable binwidth to store the width of each bin in the histogram. The y-value (1.0) indicates that each value in the bin should increment the bar height by 1.

Article last modified on January 26, 2014 at 9:59 pm.


  1. JohnB says:

    You need the “0.5*binwidth” term to shift data2.dat or else the plots will sit atop each other, is that correct? I am struggling with that problem, and your post is the only solution I’ve found. Thanks!

    1. cc says:

      No problem! You are correct, the second 0.5*binwidth term offsets the second dataset by half the binwidth so the histogram bars do not overlap. If you have n datasets, you would need to set the boxwidth to 1/n*binwidth and the offset term of the m-th dataset would be m/n*binwidth.

Leave a Reply

Your email address will not be published.