Friday, 30 March 2007

Controlling colors in gnuplot

Today my knowledgeable office mate pointed out to me that one can in fact control the colors of plots in gnuplot, which I had not managed to do before. By default, gnuplot assigns colors to your plots by the order in which you add them, the first one being red, the following green, then blue.

I had always accepted that, until I was plotting multiple data sets today, and all came out red (multiple data sets are made up of data that are in the same column, but in blocks separated by two empty lines; these blocks are accessible by index). I really needed more control here.

Now if I have
plot "datafile" index 0:1 using 1:2 with linespoints
that gives me two plots, both red. The simple trick is to append an index to the end of the command. This index refers to the standard order of addition mentioned above. So now
plot "datafile" index 0 using 1:2 with linespoints 1
plot "datafile" index 1 using 1:2 with linespoints 3
gives me a red and a blue plot. That's all! I never came across this info before.

Oh and if you wonder why block indexing starts at 0 and column indexing at 1: there is a virtual column with index 0, which holds the line numbers. Very useful. Now why the plot index starts at 1... I don't know!

No comments: