5
votes

This is my problem: I have 4 different data files; and I need to create various plots on png, using data in these files.

I would like to have all in a function that I call in a script, so I would like to put together as many common statement as I can.

The plot have different file names, but they use mostly the same settings: the legend position, the title, the axis label, the range, border line style.

What change is the data, coming from different data files, the number of lines on the same plot (some has for example 1 set of data, others has 4-5 per plot), and the color to differentiate them.

Is there a clean way to group what is similar, so I don't end up writing the same things for each of my plot? I've check the doc and I was not able to find a solution to this; since the style is set for each dataset, so I can't really do anything to group it.

Found some questions here that look similar, but the problem is totally different...I don't need to merge data from different dataset, but I need to create different plot files, which only share most of the common settings. To make a generic example, I need a way to do something like a CSS style file, so the style stay the same, but the content of the plot (and the name of the file) changes.

I am using shell script for the code; so I wrapped a gnuplot command in a shell function.

Thanks

1
Put all common commands in a separate file and use load 'settings.gp'.Christoph
Thanks! So if for example, the plot has 5 lines and the second plot has 2 lines; using the gp file that has 7 line style, will result in each plot taking the line style in a specific order? What about the name of the set output?user393267

1 Answers

9
votes

You can put all common settings in one file (lets say settings.gp) and load them from your main files with load 'settings.gp'. That works as if you would write the actual commands in place of the load command. Therefore you can define some variables before loading the settings file to change the behavior.

File settings.gp:

set terminal pngcairo
set output outfile

set style increment user
if (plotNum == 2) {
    set style line 1 lt 5
    set style line 2 lt 6
} else {
    set for [i=1:5] style line i lt i+2
}

(Note, that this kind of if statement requires gnuplot version 4.6 and newer).

File main.gp

outfile = 'first.png'
plotNum = 2
load 'settings.gp'
plot x, x**2

The command set style increment user automatically iterates over line styles instead of line types in the plot command.

That is of course only an example, basically you can include any kind of tests and conditions in you settings.gp. An other possiblity is using the call command.