Gnuplot Bar Chart Example

A bar chart compares a handful of categories by height, and in gnuplot it is one data block, the boxes style, and category labels taken straight from the data.

$DATA << EOD
Apples    42
Oranges   35
Pears     28
Plums     19
Grapes    31
EOD
set title "Fruit Sold This Week"
set ylabel "Units sold"
set boxwidth 0.6
set style fill solid 0.7 border -1
set yrange [0:*]
set grid y
unset key
plot $DATA using 2:xtic(1) with boxes linecolor rgb "#5e2d79"

How the script works

The datablock has two columns: a category name with no spaces, and its value. Category names with spaces would need quoting in the file, so single words (or underscores) keep the example simple.

using 2:xtic(1) is the bar-chart idiom: the bar heights come from column 2, while xtic(1) takes each bar’s x-axis label from column 1 of the same row. gnuplot places the bars at consecutive integer positions automatically.

set boxwidth 0.6 makes each bar 0.6 units wide, leaving gaps between bars. set style fill solid 0.7 border -1 fills the bars at 70 percent opacity with a dark border, and set yrange [0:*] forces the y axis to start at 0 — bar charts should always start at zero, because readers judge the values by bar length.

Run it online: Paste this script into the online terminal and run it — no install needed.

Variant to try

For grouped bars with two values per category, add a third column to the data and plot two series side by side by shifting positions: plot $DATA using ($0-0.2):2 with boxes, $DATA using ($0+0.2):3 with boxes with set boxwidth 0.35 — $0 is the row number, which is the bar position. For many categories, horizontal bars read better: swap the roles with set boxwidth unchanged and plot using 0:2:ytic(1)-style specs, or rotate labels with set xtics rotate by -45.

Related tutorials: Histograms in Gnuplot, Plot CSV Data in Gnuplot. See more copy-and-run scripts in the Gnuplot Examples index.

Scripts on this page are adapted from the official gnuplot 6.0 demo collection (gnuplot.info) and rewritten for this tutorial.

Similar Posts

Leave a Reply

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