2014-09-22 72 views
2

我正在做大量的使用gnuplot的情節。由於數據範圍(對於x和y軸)對於每個繪圖都是可變的,因此我需要讓gnuplot自動設置範圍和抽動。但是,我需要在圖下放置一個定義的網格,每個1/8單位的水平線和1/4個單位的垂直線。當我讓gnuplot決定放置抽動的位置時,我不知道兩個抽搐之間的距離(單位),因此,我不知道我應該在m {x | y}中抽取的細分的數量是否有所需的輸出。如何獲得自動生成的gnuplot tic之間的距離?

例如:如果我每兩個單位都有一個ytic,我需要「設置mytics 8」。如果我有一個單位,我需要「設置mytics 4」。

那麼,有什麼方法可以獲得自動放置的抽搐之間的距離?甚至是繪製抽搐的數量?

回答

5

爲了得到自動放置抽動之間的距離,使用下面的代碼(保存爲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_MAXGPVAL_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) 
+1

with ticstep.gn you saved my day。再次:)它幫助我生成定製ytics – taiko 2017-07-18 18:09:53

+0

@taiko非常好,它幫助你 – Christoph 2017-07-18 18:12:44

+0

是的。作爲提問者,我需要用不同的數據範圍生成更多的地塊。自動縮放無法令我滿意。有了STATS_min,STATS_max和你的ticstep函數的一些細節,我可以做「設置ytics STATS_min,ticstep,STATS_max」。 – taiko 2017-07-18 18:34:30