This blog contains whatever random tech stuff I'm thinking about recently. And a lot of conference reports.

Graphing chips with gnuplot

Graphing chips with gnuplot

I first learned how to use gnuplot in 2006. I remember it clearly.

Niall had suggested that there might be a linear relationship between chips and greatness. "Do you know what's great?", he would say, and I would say "chips?" and we would go spend €2.20 on salty, greasy, delicious chips from Presto beside Google Dublin and take them back to the office so that everyone else could smell them and wish they had chips too. (I  recommend doing this if you ever have the opportunity.)

Chips seemed to be correlated with greatness, but that was anecdata. We needed charts and graphs to confirm that it was true. So I made a csv file with definitely legit data showing greatness increasing over time with the availability of chips. Then I spent two hours in the office one evening figuring out how to plot the data with gnuplot. I triumphed. The hypothesis was shown to be true. We celebrated with chips.

I used gnuplot occasionally after that, always after enough of a gap that it was easiest to take the previous config and hammer on it until it did the right thing. I used it for showing how far along in a deprecation we were. I used it for getting my head around logarithmic complexity while taking an algorithms class. I graphed my sleep. Every one was copied and tweaked from the previous config.

Today I needed to make a bar chart out of some data, and reached for my trusty examples... and realised that they were in my homedir in the company I no longer work at. Noooooo. So today I learned gnuplot almost from scratch again. And you know what, it was kind of a pain. Most of the sites about it are super academic and hard to follow. 

So, for future me and for everyone who ever searches for "gnuplot non-overlapping bar chart", here is an annotated gnuplot config.

Suppose you want to draw a bar chart of two things by year. The data looks like this:

2000 125 200
2001 200 300
2002 310 4
2003 285 19
2004 275 226
2005 125 49
2006 67  170

Here's the config.


# Output a png called barchart.png. You
# could instead leave out the output filename
# here and redirect the output to a file
# like ./myconfig.gnu > barchart.png.
set terminal png
set output "barchart.png"

# Empty boxes is the default. Fill them in.
set style fill solid

# By default, adjacent boxes are extended
# in width until they touch each other. I
# want to fit two bars side by side at each
# point, and leave a little space after that, so
# I'm making the bars 0.4 units wide. (Two
# bars at 0.4 each leaves 0.2 units for space.)
set boxwidth 0.4

# Labels!
set title "A Fancy Bar Chart"
set ylabel "Counted things"
set xlabel "Year"

# Rotate the labels on the x axis. Saves space
# and looks nice.
set xtics rotate

# Kind of hacky, but I want exactly the years in
# my file (2000 to 2006) with a tiny bit of space
# on either side.
set xrange [1999.5:2006.9]

# The y axis starts at zero and ends at 350.
set yrange [0:350]

# I like grids on graphs; it makes it easier to read.
set grid

# Now read the data from numbers.dat and plot
# the graph. 'using' takes two values: the
# position on the x axis, the position on the y
# axis. "using 1:2" means the position on the x
# axis comes from the first column in
# numbers.dat and the position on the y axis
# comes from the second column. "using 1:3"
# instead sets the y axis based on the third
# column. And so on.

# First value: use the 2nd column and draw red
# (#00FF00) boxes. Second value: use the 3rd
# column and draw green (#00FF00) boxes. But
# move the x axis over a bit.

plot "numbers.dat" using 1:2 \
        title "first thing" \
        with boxes \
        linecolor rgb "#FF0000", \
 "numbers.dat" using ($1+0.4):3 \
        title "second thing" \
        with boxes \
        linecolor rgb "#00FF00"

# See the ($1+0.4) on that second one? If you're
# drawing two boxes at the same x coordinate,
# they're going to be on top of each other.
# Maybe not what you want, so this shifts the
# second box along on the x axis. The boxes
# are 0.4 units wide (the boxwidth above!), so
# moving that much makes them side by side.



This config can be runs as a script, because it has the /usr/bin/gnuplot binary on the first line as its interpreter, but you could instead write a config without that and run it with

gnuplot ./myconfig.gnu

And here's the graph it makes.


Hate bar charts? Flip the "with boxes" to "with lines" and you get


That second thing sure seems to be experiencing a decline!

And that is how to gnuplot and I'm fairly certain I will use this config as my gnuplot starting point for the rest of my life.

Delegation means not answering all the questions

Delegation means not answering all the questions

Conference Report: DevOps Days New York 2018

Conference Report: DevOps Days New York 2018