Creating a histogram with Gnuplot

Gnuplot has plenty of examples on its histograms demo page. The demos use immigration.dat as a datasource, which you can find in gnuplot’s GitHub repository: immigration.dat data source.

While the examples demonstrate many of the available features, it’s not clear what some of the specific options do. You could read the documentation, but who has time for that? Some of us are just trying to create really simple histograms and don’t need to master the nuances of gnuplot.

Here’s my sample data, colors.data, a series of attributes and a value associates with each attribute:

#Color Count
Red 45
Orange 17
Yellow 92
Green 262
Blue 129
Purple 80

Start gnuplot:

$ gnuplot

Set the style to histograms and plot the datafile:

gnuplot> set style data histograms
gnuplot> plot './colors.data' using 2:xtic(1)

The plot command above indicates that were are plotting the data from column 2, and we are using column 1 for the xtic labels (the x-axis item markers).

In the resulting histogram, the title says 'color.data' using using 2:xtic(1) which is not particularly meaningful. You can change this by including a title:

gnuplot> plot 'colors.data' using 2:xtic(1) title 'Values by Color'

That’s it! Here’s the result:

An example histogram displaying several vertical bars of different heights. In this example, the bars are hollow, defined by a purple outline.

I admit, the hollow bars were bothering me, so I make them solid:

gnuplot> set style fill solid

An example histogram showing several vertical bars of different heights. The bars are solid purple in this example.

The purple color looked great, but if you’re planning to print something in black-and-white you might want to change the color as well:

gnuplot> plot './colors.data' using 2:xtic(1) linecolor 'black' title 'Values by Color'

An example histogram showing several vertical bars of different heights. In this example, the bars are solid black.

2 thoughts on “Creating a histogram with Gnuplot”

  1. You might wonder how to save the graph produced as an image file. Following the examples on How to export Gnuplot output to png, jpg and pdf I came up with this:

    gnuplot> set terminal png size 320,240
    gnuplot> set output "histogram.png"
    gnuplot> plot './colors.data' using 2:xtic(1) linecolor 'black' title 'Values by Color'

    It no longer opens the graph in a visible window, but instead saves it to the specified file. (Using the settings from the example above, the labels on the x-axis run into each other. This could be addressed in several ways: making the graph larger, making the font smaller, or angling the xtic labels.)

    To display the output to the screen again:

    gnuplot> set terminal x11
    gnuplot> set output

Leave a Reply

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