Douglas Vechinski
Douglas Vechinski

Reputation: 21

Gnuplot script will not make correct key/legend labels for multiple data sets

I have a large number (over 2000) of data files that I am wanting to make a plots of using Gnuplot. Each file contains multiple sets of data to be plotted within the same plot. I am using a Perl script to preprocess each file into a form to be ingested by Gnuplot and to generate a Gnuplot script which is to be executed by a system call from Perl to generate an outplot file. Each data file may contain around 10 sets of data where each set represents a different value of another parameter. I am wanting to plot each set with a solid line but with a different color and to have a label in the legend/key that represents the value of this other parameter. In order to get the multiple line colors, I am at present reformatting each file so that set 1 comes first (2 columns) followed by two blank lines, followed by set 2 (2 columns) followed by two blank lines and so on. The first row of each set has a column header. The second column header is intended to be the text for the key for that set. At the beginning of the Gnuplot script I have placed a

set key autotitle columnheader

I am generating the plot with the following

plot for [i=0:9] "datafile" using 1:2:(column(-2))  with lines  lc variable

This mostly works except that all the key labels are the same and are the label from the first data set.

If I reformat the datafile so that I have 11 columns (1 X and 10 Y columns) of data and use

plot for [i=0:9] "datafile" using 1:(column(i+2)) title columnhead(i+2) with lines

I can get the key labels I want but now cannot get each line in a different color.

If these do not work, is that an alternate method to get the key labels which may be different from one file to the next.

Upvotes: 2

Views: 1763

Answers (1)

Christoph
Christoph

Reputation: 48430

You need to use index explicitely to select the data sets, then the key is correct. Consider the datafile

"first set"
1 1
2 2


"second set"
3 3
4 4

and plot this with

plot for [i=0:1] "datafile" using 1:2:(column(-2)) index i title columnhead(1) lc variable

So for your datafiles the plot command would be

plot for [i=0:9] "datafile" using 1:2:(column(-2)) index i title columnhead(1) with lines lc variable

The addition of title columnhead(1) is necessary in the example I gave, because gnuplot version 4.6 struggles on the single column head for two columns of data. In the current development version this is not necessary anymore.

Upvotes: 1

Related Questions