Search code examples
gnuplot

Adding pattern based numbers on x axis in gnuplot


I have data only for the y-axis in 30 bins. I want to plot via Gnuplot by adding the x-axis in the Gnuplot script. Range for x-axis is [-5,2] with 30 bins. How can I add a pattern or formula-based numbers on the x-axis? Or can I add this pattern for the x-axis in the data file itself using any grep command?

Data for "file1.txt" (for example) is as follows;

0.000000e+00
0.000000e+00
1.936836e+01
1.347826e+02
2.204809e+02
3.531409e+02
4.656366e+02
6.431357e+02
8.020624e+02
----
----
---- up to 30 bins

Outline for Gnuplot script is as follows;

set style line 1 dt 1 lc rgb "black" lw 2 pt 1 ps default
set style line 2 dt 2 lc rgb "black" lw 5 pt 1 ps default

pl 'file1.txt' u ($0):($1) w l smooth bezier ls 1 title "title1",\
   'file2.txt' u ($0):($1) w l smooth bezier ls 2 title "title2"

Solution

  • You may use a function to convert the x-axis values from the x index (default) to the desired values. For example, assuming 30 data records mapped linearly to [-5, 2], and indexing x values is the default [0,1,2 ...,29]

    f(x)=(x*7/29) - 5 (of course this should be -5, not -2)

    maps the interval to [-5, 2].

    Try help stats to determine number of records in data. (STATS_records)

    $Data <<EOD
    0.000000e+00
    0.000000e+00
    1.936836e+01
    1.347826e+02
    2.204809e+02
    3.531409e+02
    4.656366e+02
    6.431357e+02
    8.020624e+02
    EOD
    f(x)=(x*7/29) - 5            # map interval [0,29] to interval [-5,2] linearly
    set xrange [-5:2]            # only 9 values in this data set
    plot $Data using (f($0)):($1) with lines   # decorate as you see fit
    

    Quick sample plot