Gnuplot:图x2轴与x1轴。

时间:2022-12-13 22:21:08

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:

我在网上找到这个问题的答案似乎有些困难。题目是基本的问题,但要更具体一点,我想要有两个x轴,一个在图的顶部,这取决于底部的那个。然而,这不是一种简单的关系,即x2!=5*x1或类似的东西。该关系由数据文件本身提供。更具体地说,我有一个这样的文件:

 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.

我想在x y轴上画一个关于(wrt) P的图,在x2y轴上有T wrt V。所以x1轴会显示P的范围,x2会在x1的对应位置显示V的范围,也就是x1轴上的0。586在同一个地方的x2轴上是2.0。这在Gnuplot中是可能的吗?或者我需要和两个x轴的关系来做这个?非常感谢您的帮助。提前谢谢你。

2 个解决方案

#1


13  

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'

Gnuplot:图x2轴与x1轴。

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.

我首先在x1y1上画T。之后,我在x2y1上画出了T wrt V,并将其用于P的范围和tic位置,但是将V值作为x2轴的tic标签。这为P提供了一个线性尺度,并相应地调整了V。

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.

为了使其工作,您必须使用set autoscale xfix并设置autoscale x2fix。这使用了精确的范围,并没有将轴扩展到下一个主要的tics,这将只在x轴上完成,而不是x2轴,它有自定义的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.

当然,你也可以改变这个过程,用线性比例的V来调整P。在任何情况下,对于使用xtic()或x2tic的定制tics,这些数字就像在数据文件中被格式化一样。

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'

Gnuplot:图x2轴与x1轴。

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):

为了使一个命令只生成xtics,可以使用NaN作为y值。而且,如果只有一些定制的tics应该是标签,就需要在x2tic调用中进行适当的检查。这里,我只给所有偶数行设置标签,$0是当前行号,从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:

结果:

Gnuplot:图x2轴与x1轴。

#2


0  

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

最好的方法:在x1和y1轴上像往常一样绘制数据,但是在x2轴上添加x2tic(1)的附加标签:

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

看,在x1和x2轴上的图y1。

#1


13  

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'

Gnuplot:图x2轴与x1轴。

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.

我首先在x1y1上画T。之后,我在x2y1上画出了T wrt V,并将其用于P的范围和tic位置,但是将V值作为x2轴的tic标签。这为P提供了一个线性尺度,并相应地调整了V。

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.

为了使其工作,您必须使用set autoscale xfix并设置autoscale x2fix。这使用了精确的范围,并没有将轴扩展到下一个主要的tics,这将只在x轴上完成,而不是x2轴,它有自定义的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.

当然,你也可以改变这个过程,用线性比例的V来调整P。在任何情况下,对于使用xtic()或x2tic的定制tics,这些数字就像在数据文件中被格式化一样。

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'

Gnuplot:图x2轴与x1轴。

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):

为了使一个命令只生成xtics,可以使用NaN作为y值。而且,如果只有一些定制的tics应该是标签,就需要在x2tic调用中进行适当的检查。这里,我只给所有偶数行设置标签,$0是当前行号,从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:

结果:

Gnuplot:图x2轴与x1轴。

#2


0  

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

最好的方法:在x1和y1轴上像往常一样绘制数据,但是在x2轴上添加x2tic(1)的附加标签:

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

看,在x1和x2轴上的图y1。