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!


2 Responses to “Controlling colors in gnuplot”

  1. 1 yungchin 26 February 2008 at 16:52

    Why is this post suddenly getting a lot of visits? Is Gnuplot getting more popular? Or is WordPress just better at getting pages indexed than Blogger was (where this post was first put up, almost a year ago)?

    Anyway, if you happen to stop by, let me know :)

  2. 2 guest 26 September 2008 at 15:59

    because evereyone expects a list colors and the assoicated indices..

Comments are currently closed.

%d bloggers like this: