Gnuplotting

Create scientific plots using gnuplot

April 16th, 2014 | 4 Comments

Gnuplot comes with the possibility of plotting histograms, but this requires that the data in the individual bins was already calculated. Here, we start with an one dimensional set of data that we want to count and plot as an histogram, similar to the hist() function we find in Octave.

Histogram of angle data

Fig. 1 Two different distributions of measured angles. (code to produce this figure, hist.fct, data)

In Fig. 1 you see two different distributions of measured angles. They were both given as one dimensional data and plotted with a defined macro that is doing the histogram calculation. The macro is defined in an additional file hist.fct and loaded before the plotting command.

binwidth = 4
binstart = -100
load 'hist.fct'
plot 'histogram.txt' i 0 @hist ls 1,\
     ''              i 1 @hist ls 2

The content of hist.fct, including the definition of @hist looks like this

# set width of single bins in histogram
set boxwidth 0.9*binwidth
# set fill style of bins
set style fill solid 0.5
# define macro for plotting the histogram
hist = 'u (binwidth*(floor(($1-binstart)/binwidth)+0.5)+binstart):(1.0) smooth freq w boxes'

For a detailed discussion on why @hist calculates a histogram you should have a look at this discussion and the documentation about the smooth freq which basically counts points with the same x-value. The other settings in the file define the width of a single bin plotted as a box and its fill style.

Histogram of angle data

Fig. 2 Two different distributions of measured angles. The bins of the histograms are shifted to be centered around 0°. (code to produce this figure, hist.fct, data)

It is important that the two values binwidth and binstart are defined before loading the hist.fct file. These define the width of the single bins and at what position the left border of a single bin should be positioned. For example, let us assume that we want to have the bins centered around 0° as shown in Fig. 2. This can be achieved by settings the binstart to half the binwidth:

binwidth = 4
binstart = -2
load 'hist.fct'
plot 'histogram.txt' i 0 @hist ls 1,\
     ''              i 1 @hist ls 2

4 Comments

  1. Nicolás says:

    Thanks for doing all this tutorials man!

  2. Arthur says:

    Hi,

    This website is just awesome !!! You’ve done a great job. Thanks for that.

    But i wonder how to draw a gaussian on the graph (by using a fit curve) with only an one dimensional set of data ?

    Thanks.

    Arthur

  3. hagen says:

    Hi Arthur.

    It is possible, but complicated. You can not use smooth together with fit. Hence, we first need to store our data to a temporary file and fit them afterwards. The every command is used to ignore the last line in the hist.txt file, which is not needed. This will then result in Fig. 3 at the end.

    # store data in a temporary file for fitting
    set table 'hist.txt'
    plot 'histogram.txt' i 0 @hist ls 1
    unset table
    # fit Gaussian
    Gauss(x) = a/(sigma*sqrt(2*pi)) * exp( -(x-mu)**2 / (2*sigma**2) )
    a = 30.
    mu = 3.
    sigma = 3.
    fit Gauss(x) 'hist.txt' every ::0::7 u 1:2 via a,mu,sigma
    # plot
    plot 'hist.txt' every ::0::7 using 1:2 w boxes ls 1,\
         Gauss(x) w lines ls 2 lw 2
    
    Histogram together with Gaussian fit of angle data

    Fig. 3 Distributions of measured angles and a fitted Gaussian distribution. (code to produce this figure, hist.fct, data)

  4. Arthur says:

    Hi,

    Thanks a lot ! It is working very well. I didn’t know that it was possible to store the data. It will be very useful for my future plots.

    I hope to see more amazing plots on your website, because I learned lots of things here.

    Arthur

Leave a Reply