Gnuplot: Plot x2 axis with respect to x1 axis

前端 未结 2 1453
抹茶落季
抹茶落季 2020-12-31 18:00

I seem to be having some difficulty finding the answer to this question online. The title is the basic question, but to be more specific I would like to have two x axes, one

2条回答
  •  有刺的猬
    2020-12-31 18:16

    Here is how you can achieve this. I first show you the script and the result, and later explain the steps:

    reset
    set xtics nomirror
    set x2tics
    set autoscale xfix
    set autoscale x2fix
    set xlabel 'P'
    set ylabel 'T'
    set x2label 'V'
    plot 'data.txt' using 3:2 with linespoints ps 2 lw 2 title 'T wrt P', \
         '' using 3:2:x2tic(1) axes x2y1 with points ps 2 lw 2 title 'T wrt V'
    

    enter image description here

    I first plot T wrt P on x1y1. Afterwards I plot T wrt V on x2y1 and use for this the range and tic positions of P, but use the V values as tic labels for the x2 axis. This gives a linear scale for P and adapts V accordingly.

    In order for this to work you must use set autoscale xfix and set autoscale x2fix. This uses the exact ranges and does not expand an axis to the next major tics, which would be done only for the x axis, but not for the x2 axis, which has custom tics.

    You could of course also reverse the process and use a linear scale for V and adapt the P tics. In any case, for the custom tics, which are placed with xtic() or x2tic, the numbers are used like they are formatted in the data file.

    reset
    set xtics nomirror
    set x2tics 1
    set autoscale xfix
    set autoscale x2fix
    set xlabel 'P'
    set ylabel 'T'
    set x2label 'V'
    plot 'data.txt' using 1:2:xtic(3) with linespoints ps 2 lw 2 title 'T wrt P', \
         '' using 1:2 axes x2y1 with points ps 2 lw 2 title 'T wrt V'
    

    enter image description here

    Here, the points are shown for both plot lines, to demonstrate, that they really coincide.

    In order to have the one command only generating the xtics, one can use NaN for the y-value. And if only some of the custom tics should be labels, one needs an appropriate check in the x2tic call. Here, I set labels only for all even rows $0 is the current row number, starting from 0):

    reset
    set xtics nomirror
    set x2tics
    set autoscale xfix
    set autoscale x2fix
    set xlabel 'P'
    set ylabel 'T'
    set x2label 'V'
    plot 'data.txt' using 3:2 with linespoints ps 2 lw 2 title 'T wrt P', \
         '' using 3:(NaN):x2tic((int($0) % 2) ? '' : stringcolumn(1)) axes x2y1 t ''
    

    With the result:

    enter image description here

提交回复
热议问题