Wednesday, 22 September 2010

Four gnuplot Tricks

Depending on what you do on a day-to-day basis, you might use the plotting program gnuplot. If you film dolphins underwater, probably not. But if, like me, you have to digest pages and pages of columned simulation data, maybe you do, and in that case I have a few tips and tricks you might not know about.

I'm going to presume that you're reading this as someone quite familiar with gnuplot. It might not be pretty but it's quick and shows me what I need to see. (This sentence could be removed from context with disastrous consequences...) I don't use it for producing plots for publication or presentation but if I just need a quick look at what's going wrong with my PhD, gnuplot fits the bill. With a few abbreviations to speed up the process, it's actually really useful.

All of these tips are from my daily routine which takes place in Bash terminals, so I can't guarantee they'll work on every system. In particular, I'm not sure about how much will work with Windows. But why are you trying to do work in Windows?

Plot Window Shortcuts

There are a couple of commands you can issue while the plot window is selected. For example, pressing "e" issues a replot command. For a complete list, select a plot window and press "h". The list will appear in the command window.

Terminal Abbreviations

This is a really simple trick that I'm including just in case you missed out on it. One can abbreviate just about every terminal command in gnuplot. For example,

plot "file" using 1:2

can be shortened to

p "file" u 1:2

If you're used to hacking away at the terminal, this is a must. What's more, this isn't from a standard list: as long as the abbreviation is unambiguous in context, it should work. So p above could just as well be pl.

Using Shell Commands in the Filename

What if you only want to plot from the last 200 lines of the file? There are some native gnuplot instructions that will do this but an alternative way, for the example above, is

p "< tail -200 file" u 1:2

See what I did there? You can effectively construct a new output stream with whatever terminal command you'd like. I personally have a formidable armoury of sed, awk and egrep commands that I use to work with my data. Okay, formidable for the PhDemon rather than the US Marine Corps, but I don't have to wrestle with the US Marines Corps on a daily basis.

Command History Search

A familiar trick for Bash users is the ability to search your history for a previously issued command. Well, if you didn't know about that, now you do. If you don't feel like pressing the up arrow 206 times, press "Ctrl+R" to get a mini-prompt which you can use to search all your previous commands. I suspect this is an inherited Bash feature, so I'm not sure if it works in all circumstances but it definitely works in GNOME.

No comments:

Post a Comment