Search code examples
gnuplot

x axis on top of graph in gnu plot


I'm able to put an x axis on the top of a graph in gnu plot via

set x2label "label" and set x2tics

I have a column of data of data1 values and I'd like to plot these values multiplied by two on the upper x axis against data data2 in the lower x axis. Both data1 and data2 are in the same data file. Here is some sample data

data2     data1
20        1.2e-2
40        3.0e-3
60        1.4e-3
...          ...

I'd like to plot 2*data1 on upper axis against data2 on lower axis. Preferably, I'd like to just put a tick mark on the 2*data1 axis for every data2 value. On the y axis I will plot some other quantity against data2 but all I want to ask about here is how to plot x2 versus x1.

Thanks!


Solution

  • still guessing what exactly you want. Maybe we'll find out faster with this example.

    reset session
    
    $Data <<EOD
    # x2  x1      y
    20    1.2e-2  1
    40    3.0e-3  2
    60    1.4e-3  3
    EOD
    
    set key top center
    set xtics nomirror
    set x2tics nomirror
    
    plot $Data u ($1*2):3 axes x2y1 w lp pt 7 title "y-data vs. x2-axis", \
          '' u 2:3 axes x1y1 w lp pt 7 title "y-data vs. x1-axis"
    

    Result:

    enter image description here

    Edit:

    You can link x1 and x2 axes via a function (in the example below conversion between nm and eV) and then set your x2tics as desired.

    1. Graph1: corresponding "odd" values from x1,
    2. Graph2: "even" values by given interval x2tics 0.2,
    3. Graph3: manual setting of x2tics.

    Example:

    ### linked axes x1, x2
    reset session
    
    set xlabel "Wavelength / nm"
    set xtics nomirror
    set x2label "Energy / eV"
    set x2tics nomirror
    set link x via 1239.8/x inverse 1239.8/x
    
    set ylabel "Intensity / a.u."
    set ytics 0.2
    set samples 400
    Spectrum(x) = exp(-(x-500)**2/(400))
    
    set xrange[380:780]
    
    set multiplot layout 3,1
        set format x2 "%.2f"
        plot Spectrum(x) w l title "Spectrum"
    
        set format x2 "%.1f"
        set x2tics 0.2
        replot
    
        set x2tics ()
        myTics = "1.7 1.8 1.9 2.0 2.1 2.3 2.5 2.7 3.0"
        do for [i=1:words(myTics)] { set x2tics add (word(myTics,i) real(word(myTics,i))) }
        replot
    
    unset multiplot
    ### end of code
    

    Result:

    enter image description here