在gnu图中无法通过与下轴链接获得上轴值

时间:2019-05-28 16:03:09

标签: formatting gnuplot

我正在尝试在gnu图中链接上轴和下轴,即在给定上轴量和下轴量之间的关系的情况下,在上轴上打印出值。它们之间的关系是

x2 = 2*c**2/(2*x1**2-c**2)c=1.548458。 以下内容就足够

set xlabel "Quantity lower"
set xtics nomirror
set x2label "Quantity Upper"
set x2tics nomirror
set link x2 via 2*(2*1.548458)**2/(2*x**2-(2*1.548458)**2) inverse sqrt((2*1.548458)**2*(2+x)/2/x)

#set link x via 1239.8/x inverse 1239.8/x

set ylabel "Quantity y"
set ytics 0.2
set samples 400
Spectrum(x) = exp(-(x-500)**2/(400))

set xrange[30:2000]

set format x2 "%.2f"
plot Spectrum(x) w l title "Spectrum"

我还通过

记录了x轴的格式

set logscale x set format x "10^{%L}"

但是无论我是否包括日志格式,我都无法在上轴上打印出任何有意义的值。 (用于y的功能仅用于说明目的,在我的实际脚本中y的值取决于从文件中读取数据)

我想在上轴上打印出两个x = 100、1000个值的值。谢谢!

1 个答案:

答案 0 :(得分:0)

我不知道如何自动设置抽动。但是至少您可以显式设置它们。

您想要将x2抽头设置为给定的x1值。由于set x2tics命令在x2坐标中取得tic位置,因此您可以将给定的x1值转换为x2值。我已经修改了您的脚本以说明我的意思:

set xlabel "Quantity lower"
set xtics nomirror
set x2label "Quantity Upper"
set x2tics nomirror

# avoid warnings by setting the xrange before linking the axes
set xrange[30:2000]

# setup the link functions
c = 1.548458 # or 2*1.548458 ? 
link_x1_x2(x) = 2*c**2/(2*x**2-c**2) 
link_x2_x1(x) = c*sqrt((2+x)/(2*x))

set link x2 via link_x1_x2(x) inverse link_x2_x1(x)

# you want to set x2 tics at x1 = 100 and x1 = 1000
# check the corresponding x2 values 
print "x2 = x1( 100) = ", link_x1_x2(100)
print "x2 = x1(1000) = ", link_x1_x2(1000)

# set the tics with formatted tic labels as an example
set x2tics ("a: %.2e" link_x1_x2(100), "b: %.2e" link_x1_x2(1000))

set ylabel "Quantity y"
set ytics 0.2
set samples 400
Spectrum(x) = exp(-(x-500)**2/(400))


# this seems not to work with explicit tics
# set format x2 "%.2f"

plot Spectrum(x) w l title "Spectrum"

这是输出:

x2 axis linked to x1 axis

如果需要对数轴并且set link命令不起作用,则可以通过用set x2range显式设置x2axis属性来替换它。请参阅修改后的脚本:

set xlabel "Quantity lower"
set xtics nomirror
set x2label "Quantity Upper"
set x2tics nomirror


# setup the link functions
c = 1.548458 # or 2*1.548458 ?
link_x1_x2(x) = 2*c**2/(2*x**2-c**2) 
link_x2_x1(x) = c*sqrt((2+x)/(2*x))

xmin = 30
xmax = 2000
set xrange[xmin:xmax]
set x2range[link_x1_x2(xmin):link_x1_x2(xmax)]

set logscale x 
set logscale y 
set logscale x2

# set the tics at x1 = 100 and x1 = 1000
set x2tics ("a: %.2e" link_x1_x2(100), "b: %.2e" link_x1_x2(1000))

set ylabel "Quantity y"
#set ytics 0.2 # this must be improved for logarithmic plotting
set samples 400
Spectrum(x) = exp(-(x-500)**2/(400))

plot Spectrum(x) w l title "Spectrum"

这是结果:

manual linkage

请注意,该函数在x1 = 1000处的下降是由数字下溢引起的。