0

Situation:

I have a rather complicated yet generic gnuplot script to visualize multiple measurement values. However there still is a vast amount of code duplication for the actual plot commands that I want to avoid.

Minimal example (many variables and options omitted) could be as follows:

#define variables here
folder="folder/"
algo="Version1 Version2"

#label:start
#plot first specific e.g. runtime of a program
set output folder."/Runtime".fileext
plot for[i=1:words(algo)] \
   folder.word(algo,i).".data" using 1:2 .....

#plot antoher specific e.g. Cache Misses
set output folder."/CacheMisses".fileext
plot for[i=1:words(algo)] \
   folder.word(algo,i).".data" using 1:3 .....

#label:end

#change some variables e.g.
algo="Version3 Version4"

#repeat everything between label:start and label:end

In my real script there are about 20 plots in one block (between label:start and label:end) and a high number of blocks (with different variables).

Question:

Is there an easy way to substitute blocks of gnuplot 'code' with a generic or text-replacement method to avoid duplication?

My desired script would look like this:

#define variables here
folder="folder/"
algo="Version1 Version2"

magical_command(...)

#change some variables e.g.
algo="Version3 Version4"

magical_command(...)

I have already found out:

  • the gnuplot macros do not support variadic texts (i.e. varaibles) inside.
  • functions need a plot command; which reduces only parts of the duplication
  • my best guess at the moment would be a call to commandline (e.g. with cmd) to call another gnuplot-script and parameterize the needed variables (Not sure about options though)

1 Answer 1

1

I suppose the easiest would be to call the gnuplot script with some argument:

gnuplot -e "algo='Version1 Version2'" yourgnuplotscript.gp

where yourgnuplotscript.gp contains everything between label:start and label:end.

If you need to call this for many algo variables, you could use a bash script with a for loop that runs over those variables.

Sign up to request clarification or add additional context in comments.

4 Comments

This actually works for the defined variables. e.g. -e \"algo='".algo."'\". Is there also any way to pass through the options, like set terminal... or set xkey without specifiying them as -e parameters again?
I don't understand your question in the comment... you can pass multiple options with -e if you separate them with ;
I am able to pass all my user defined variables with -e. However I do not want (if possible) to repeat the options to be set once in the gnuplot file. These are e.g. the set terminal, set style, set key... Otherwise I have to repeat the whole Header of my 'outer' plot file in the subcall, which would be undesireable.
ah... well, why don't your write them inside the main gnuplot file ? If they remain identical for all files, there's no point in passing them as an option. So the options that you pass to the main gnuplot file are the ones that changes. The ones that do not change can directly be written inside the gnuplot file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.