如何获得自动生成的gnuplot tic之间的距离?
问题描述:
我正在做大量的使用gnuplot的情节。由于数据范围(对于x和y轴)对于每个绘图都是可变的,因此我需要让gnuplot自动设置范围和抽动。但是,我需要在图下放置一个定义的网格,每个1/8单位的水平线和1/4个单位的垂直线。当我让gnuplot决定放置抽动的位置时,我不知道两个抽搐之间的距离(单位),因此,我不知道我应该在m {x | y}中抽取的细分的数量是否有所需的输出。如何获得自动生成的gnuplot tic之间的距离?
例如:如果我每两个单位都有一个ytic,我需要“设置mytics 8”。如果我有一个单位,我需要“设置mytics 4”。
那么,有什么方法可以获得自动放置的抽搐之间的距离?甚至是绘制抽搐的数量?
答
为了得到自动放置抽动之间的距离,使用下面的代码(保存为ticstep.gp
):
xr = abs(max_value - min_value)
power = 10.0 ** floor(log10(xr))
xnorm = xr/power # approximate number of decades
posns = 20.0/xnorm;
if (posns > 40) {
tics = 0.05
} else {
if (posns > 20) {
tics = 0.1
} else {
if (posns > 10) {
tics = 0.2
} else {
if (posns > 4) {
tics = 0.5
} else {
if (posns > 2) {
tics = 1
} else {
if (posns > 0.5) {
tics = 2
} else {
tics = ceil(xnorm)
}
}
}
}
}
}
ticstep = tics * power
这应该是等效于内部的gnuplot-代码来确定ticstep(见axis.c, line 589。
只获取ticstep,你可以使用stats
来获取相应的数据值:
stats 'file.txt' using 1 noutput
max_value = STATS_max
min_value = STATS_min
load 'ticstep.gp'
print ticstep
要获得绘制的抽搐数量,您需要自动扩展轴限制(除非您使用set autoscale fix
)。为此,您可以使用unknown
终端进行绘图以获取GPVAL_Y_MAX
和GPVAL_Y_MIN
:
set terminal push # save current terminal
set terminal unknown
plot 'file.txt' using 1
set terminal pop # restore terminal
max_value = GPVAL_Y_MAX
min_value = GPVAL_Y_MIN
load 'ticstep.gp'
print sprintf('ticstep = %f', ticstep)
numtics = int((xr/ticstep) + 1)
print sprintf('numtics = %d', numtics)
with ticstep.gn you saved my day。再次:)它帮助我生成定制ytics – taiko 2017-07-18 18:09:53
@taiko非常好,它帮助你 – Christoph 2017-07-18 18:12:44
是的。作为提问者,我需要用不同的数据范围生成更多的地块。自动缩放无法令我满意。有了STATS_min,STATS_max和你的ticstep函数的一些细节,我可以做“设置ytics STATS_min,ticstep,STATS_max”。 – taiko 2017-07-18 18:34:30