Feb 12, 2009

gnuplot plumbing

For over 2 weeks, I've been trying to figure out what's wrong with gnuplot...

Firstly, some background for my readers: gnuplot is an open-source program that plots stuff: graphs, data, curves, surfaces etc. Not only does it have a GUI that allows users to plot stuff, it allows programmers to send commands directly into the program through a mechanism called piping. For those who has some skill in *nix shells, you probably know a little about it, since it can achieved by the "|" (pipe) operator.

Recently, I have been working on a few Python scripts that analyze some experiment data. Once the analysis was done, it should display a graph of the data, using gnuplot. To communicate to gnuplot, I used the program "pgnuplot", which is a pipeline interface for gnuplot (and is bundled with gnuplot). However, no matter what I tried, the code always fails with a strange error:
[IOError 22: Invalid Argument]
I thought I was doing something wrong, and tried different methods several times. Later, I discovered (through Command Prompt) that the command:
echo plot x | pgnuplot
for some reason fails if the working directory is not the directory of pgnuplot, but works fine if I am in the right directory. I later tried changing the working directory in Python, but that didn't work either.

I nearly gave up on this idea, but I soon found something else strange. I used a snippet of code that searches the PATH environment variable of the OS, so that it can retrieve the absolute path of the pgnuplot executable. And guess what I saw? It was pointing to something entirely unexpected: a different file that is also named "pgnuplot". So that's what it has been doing!

All this time, my Python program was referring to the wrong pgnuplot executable, so all I needed to do was to use the correct absolute path, and - voilĂ  - it worked like a charm. No more funny-looking error messages when I try to pipe to pgnuplot.

That sure made my day.

0 comments:

Post a Comment