Gnuplot 非线性次级xlabel

Gnuplot 非线性次级xlabel,gnuplot,Gnuplot,我想绘制一个函数/数据集,其中次x轴是主x轴的函数,例如1/x(非线性)。原因是它们都是有意义的参数。我可以使用“函数”正确设置范围,但两者之间的值不同。下图显示了问题所在: 这里的问题是,x2轴是线性的(这是正确的英语吗?)。示例:与1.6相对的tic现在为0.7,但在这种情况下应为0.625 要复制此脚本,请执行以下操作: xmin=1. xmax=2. set xlabel "x" set xrange [xmin:xmax] set x2tics set x2label "1/x"

我想绘制一个函数/数据集,其中次x轴是主x轴的函数,例如
1/x
(非线性)。原因是它们都是有意义的参数。我可以使用“函数”正确设置范围,但两者之间的值不同。下图显示了问题所在:

这里的问题是,x2轴是线性的(这是正确的英语吗?)。示例:与1.6相对的tic现在为0.7,但在这种情况下应为0.625

要复制此脚本,请执行以下操作:

xmin=1.
xmax=2.

set xlabel "x"
set xrange [xmin:xmax]

set x2tics
set x2label "1/x"
set x2range [1/xmin:1/xmax]

plot x

除了对数,是一种改变轴缩放/标记的常规方法吗?

您可以使用
do for
方法来循环手动标签设置(
set x2tics add
)。以下是您提供的经过编辑的最小示例(有关解释,请参阅脚本中的注释):

xmin=1。
xmax=2。
inc=0.2#用于对tics进行更多控制
f(x)=1./x#定义次轴的函数
fplot(x)=x#用于绘图的函数
设置xlabel“x”
设置xrange[xmin:xmax]
设置xtics xmin,inc,xmax#也指定xtics的实际位置
ntics=(xmax xmin)/inc+1#计算tic的数量
inc2=(f(xmin)-f(xmax))/(ntics-1)#计算x2tics的增量
fmin=f(xmin);fmax=f(xmax)
设置x2tics fmin,-1*inc2,fmax#设置x2tics,标签将被重写
#以下循环显示了TIC的数量
#命令将重复执行
#使用算术计算值和位置
#您需要将tic标签号转换为字符串
[i=1:ntics+1]的do{
xtic=xmin+(i-1)*inc
x2ticpos=f(xmin)-(i-1)*inc2
设置x2tics add(sprintf(“%.3g”,f(xtic))x2ticpos)
}
设置X2范围[f(xmin):f(xmax)]
设置yrange[fplot(xmin):fplot(xmax)]#您还应该指定yrange,以避免意外
地块(x)
你会得到这样的图:


我复制了您的脚本,但我缺少“0.500”tic。如果我必须循环运行直到
ntics+1
,这是可以的。如果您使用的是4.7开发版本,您可以使用
set link
,请参见@Christoph。唯一的区别是,在我的情况下,曲线不应该改变,但您的链接看起来很有趣!这是为了显示命令
set link
。在这个回答中,它被用作黑客。有关与您类似的用例,请参阅。
xmin=1.
xmax=2.
inc=0.2 # for more control over the tics

f(x)=1./x  # Function that defines the secondary axis
fplot(x)=x # Function that is plot

set xlabel "x"
set xrange [xmin:xmax]
set xtics xmin,inc,xmax # specify the actual location for xtics, too

ntics = (xmax-xmin)/inc + 1 # calculate the number of tics
inc2 = (f(xmin)-f(xmax))/(ntics-1) # calculate increment for x2tics

fmin=f(xmin); fmax=f(xmax)
set x2tics fmin,-1*inc2,fmax # set x2tics, labels will be rewritten

# the following loops through the number of tics
# the <set x2tics add ("x2" x2)> command will be repeated
# use arithmetic to calculate the values and the locations
# you need <sprintf> to convert the tic label number into a string

do for [i=1:ntics+1] {
 xtic=xmin+(i-1)*inc
 x2ticpos=f(xmin)-(i-1)*inc2
 set x2tics add (sprintf("%.3g",f(xtic)) x2ticpos)
 }


set x2range [f(xmin):f(xmax)] 

set yrange[fplot(xmin):fplot(xmax)] # you should also specify the yrange, to avoid surprises

plot fplot(x)