Gnuplot: Plot x2 axis with respect to x1 axis

19,735

Solution 1

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

Solution 2

The best way: Plot your data as usual on the x1 and y1 axes, but place additional labels on the x2-axis with x2tic(column_number):

set x2tics
set xtics nomirror
plot 'data.txt' using 3:2:x2tic(1) w lp

see: Plot y1 in x1 with respect to x2 axis

Share:
19,735
falconskull
Author by

falconskull

Updated on July 23, 2022

Comments

  • falconskull
    falconskull almost 2 years

    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 at the top of the figure that is dependent on the one at the bottom. However, this is not a simple relationship, i.e. x2!=5*x1 or something like that. The relationship is given by the data file itself. So to be more specific I have a file that looks something like this:

     V     T       P
    2.0   15.0   0.586
    3.0   17.4   0.798
    4.0   25.3   1.023
    5.0   28.9   1.124
    6.0   30.2   1.456
    

    I would like to make a plot of T with respect to (wrt) P on the x1y1 axes and have T wrt V on the x2y1 axes. So the x1 axis would display the P range and x2 would display the V range in the corresponding places of x1, i.e. 0.586 on x1 axis would have 2.0 on x2 axis at the same place. Is this actually possible in Gnuplot or do I have to have a relationship with the two x axes to do this? Any help would be greatly appreciated. Thank you in advance.

  • falconskull
    falconskull over 10 years
    Thank you Christoph, this is exactly what I needed. I had a feeling this was going to be something as simple as adding another thing to the using 3:2, but I did not think to use x2tic. I was trying far more complicated ways. I have 2 other related questions if you have a moment. Is there a way to not display every single tic on the x2 axis? I tried the basic x2tics 2,2 and x2tics (2, 4, 6), but neither seems to affect the tic labels. Also is it possible to add the tics, but not the points (other than making the title for the second plot blank and using the same style for both plots)?
  • Christoph
    Christoph over 10 years
    @falconskull Using set x2tics ... doesn't work, because you have custom labels. See last example in the edit how to do it.
  • Nikko
    Nikko almost 9 years
    Dear @Christoph really nice answer. This is what I need since long time ago. I've tried to apply the method to my plot but I cannot make it as good as I want. This is my result: link I have two questions if you want to help me (sorry to post them here). I've tried the condition to plot specific labels but is not good enough for me... Can I specify the labels by hand? And the other is can I also specify how many x2tics I plot? (there are too many in my plot) Thank you very much!
  • Christoph
    Christoph almost 9 years
    @Nikko In general you can add labels manually to the x2axis with set x2tics add ("name" <pos>, ...), where <pos> is the coordinate on the x2-axis. With the specific method shown in this answer using ...:x2tic(...) here you cannot select how many x2tics are drawn. For every data point you get a tic, but only some of them are labelled. If you need very few tics only, you must add them by hand.
  • Nikko
    Nikko almost 9 years
    Therefore I cannot use the method if I have to add them by hand right? =(
  • Christoph
    Christoph almost 9 years
    @Nikko Yes, I guess so