非线性二级xlabel

时间:2014-02-13 08:28:34

标签: gnuplot

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

Sample figure

这里有什么问题,是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

除了对数以外,是一种改变轴缩放/标记的通用方法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用do for方法循环手动标签设置(set x2tics add)。这是您提供的已编辑的最小示例(请参阅脚本中的注释以获得解释):

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)

你得到的情节是:

manual x2 ticks