Search code examples
gnuplotpost-processing

Define and use vector for graph ticks


I like to store everything I can in a variable since each gnuplot script I make generates tens of plots at once and it makes things easier to track. Here is a sample of one plot (variable of interest: ytics):

# Setup style
set terminal pngcairo dashed
unset key
set style line 1 pointtype 7 pointsize 0.3 linecolor rgb "black"

# Setup the plots' ytics
ytics_H2 = (0,0.002,0.004,0.006,0.008,0.010,0.012);

# Store the range for each variable
min_T  = 200; max_T  = 1800;
min_H2 = 0;   max_H2 = 0.012;



# Plot
set output 'my_output_H2.png'
set ytics ytics_H2
set xrange [min_T :max_T ]
set yrange [min_H2:max_H2]
plot 'scatter.dat' using 1:2 with points linestyle 1

Here is the result: enter image description here

As you can see, only the last tick gets printed. If I replace the variable ytics by the vector to which it is set to, everything works as expected.


Solution

  • For such use cases gnuplot has macros:

    set macros # necessary only for v < 5.0
    ytics = "(1, 5, 8)"
    set ytics @ytics
    plot x
    

    Script output with gnuplot 5.0

    In order to use macros, you must define a string variable which contains the command parts you want to use at a later point, here ytics = "(1, 5, 8)". Later you can use its content with @ytics.

    The important fact here is, that gnuplot first replaces @ytics with the content of the string variable ytics, i.e. expands set ytics @ytics to set ytics (1, 5, 5) and only then executes the whole command.