2013-02-13 33 views
0

我繪製了一個圖,我想將ytics顯示爲x的函數。例如: 我繪製x^2,我想爲0,1,4,9 ...顯示ytics。有沒有辦法自動做到這一點,或者我必須手動設置y軸的每一個tic?我在定義ytics時試圖設置一個函數,但gnuplot不接受它。 感謝您的任何幫助gnuplot中ytics的增量

回答

2

您可以使用for循環。當然,在這裏你需要事先知道你的X-範圍:

f(x)=x**2 
set ytics (sprintf('%f',f(-10)) f(-10)) 
set for [i=-9:10] ytics add (sprintf('%f',f(i)) f(i)) 
plot f(x) 
+0

你剛剛打敗了我!更緊湊的方式也是如此。我不知道'add'字符串命令。 – andyras 2013-02-13 16:11:44

+0

@andyras - 與[今天早上我回答的另一個]非常相似(http://stackoverflow.com/a/14847447/748858)。 – mgilson 2013-02-13 19:14:03

+0

非常感謝!這正是我正在尋找的!謝謝!!! – Paoligno90 2013-02-14 00:01:06

0

這裏是我的半自動的答案,使用for循環來構建ytics字符串:

#!/usr/bin/env gnuplot 

set terminal pngcairo 
set output 'test.png' 

# x range 
xmin = 0 
xmax = 10 
set xrange [xmin:xmax] 

# define function of x to make plot, define tics 
yfn(x) = x**2 

# integer counting over tic positions 
imin = int(xmin) 
imax = int(xmax) 

# build tics string 
tix = '("'.sprintf('%d', yfn(imin)).'" '.sprintf('%d', yfn(imin)) 
do for [i=(imin+1):imax] { 
tix = tix.', "'.sprintf('%d', yfn(i)).'" '.sprintf('%d', yfn(i)) 
} 
tix = tix.')' 

set macros 
# set ytics using macro expansion 
set ytics @tix 

plot yfn(x) 

這是結果:

enter image description here