Plotting a Histogram with Gnuplot

Menu

Brief (?) tutorial on creating histograms with Org Mode and Gnuplot.

We start with a table with some random data.

Year Books Borrowed Books Bought Books Sold
2012 8 10 2
2013 8 36 1
2014 13 3 10
2015 19 2 15
2016 5 10 6
2017 6 14 4
2018 9 7 9
2019 4 6 5
2020 1 3 0

It seems to be important that the header row is not separated from the rest of the table, or Org Mode will export from the second line, with no header row.

Simple Histogram

We can now plot the data using Gnuplot with a source block.

In the source block we bind barplot to the data contained in the books-per-year table. Org Mode will generate a temporary file with the table data and pass it to Gnuplot.

Make sure you start your script with reset, since Org Mode reuses the same Gnuplot session and some settings will persist across different source blocks.

# reset is very important or Gnuplot will remember previous settings
reset 

set boxwidth 0.5
set grid ytics linestyle 0
set style fill solid 0.20 border 

set terminal svg size 1200,800 font "Arial,10"

set title "Books Borrowed"
set xlabel "Year"
set ylabel "Number of Books"

plot barplot using 1:2:xtic(1) with boxes lc rgb "#0045FF" title "These are the books I borrowed", \
     barplot using 1:($2+0.25):2 with labels title ""

histogram-with-labels.svg

Row stacked Histogram

We can also plot different data series.

In this case we declare that the results are to be written to a file and no output should be written in the buffer. This is a trick to control better how to input the image in the buffer: we need to add it manually, but we can add the attributes we prefer.

# very important or Gnuplot will remember previous settings
reset 

set boxwidth 0.5
set grid ytics linestyle 0
set style fill solid 0.20 border 

set style data histogram
set style histogram rowstacked

set terminal svg size 1200,800
set xlabel "Year"
set ylabel "Number of Books"

plot rowstacked using 2:xtic(1) title columnhead lc rgb("#FF0000"), \
     ''         using 3:xtic(1) title columnhead lc rgb("#0000FF"), \
     ''         using ($0 - 1):($2 - 0.8):2 with labels textcolor rgb("#FF0000") notitle, \
     ''         using ($0 - 1):($3 + $2 - 0.8):3 with labels notitle, \
     ''         using ($0 - 1):($3 + $2 + 0.5):(sprintf("Sum: %d", $2 + $3)) with labels font 'Arial-Bold' textcolor rgb("#800080") notitle

histogram-rowstacked.svg

Clustered Histogram

reset
set boxwidth 0.6
set grid ytics linestyle 0
set style fill solid 0.20 border 

set style data histograms
set style histogram clustered gap 1

set terminal svg size 1200,800
set xlabel "Year"
set ylabel "Number of Books"
plot barplot using 2:xtic(1) title "Borrowed" lc rgb("#0000AA"), \
     ''      using 3 title "Bought" lc rgb("#00AA00"), \
     ''      using 4 title "Sold" lc rgb("#AA0000"), \
     ''      using ($0 - 1. / 4):($2 + 0.5):2 with labels title "" textcolor rgb("#0000AA"), \
     ''      using ($0):($3 + 0.5):3 with labels title "" textcolor rgb("#00AA00"), \
     ''      using ($0 + 1. / 4):($4 + 0.5):4 with labels title "" textcolor rgb("#AA0000")

histogram-clustered.svg

Histogram Sets with New-histogram

set boxwidth 0.5
set grid ytics linestyle 0
set style fill solid 0.20 border 

set style data histograms
set style histogram clustered

set terminal svg size 1200,800
set xlabel "Year"
set ylabel "Number of Books"
plot newhistogram "Group 1", barplot using 2:xtic(1) title "Borrowed", '' using 3 title "Bought", \
     newhistogram "Group 2", barplot using 4:xtic(1) title "Sold"

newhistogram.svg