Reputation: 1
I plotted a graph and I'd like to show ytics as a function of x. For example: I plot x^2 and I'd like to show ytics for 0,1,4,9... . Is there any way to do this automatically or I have to set manually every tic in y-axis? I tried to set a function when defining ytics but gnuplot doesn't accept it. Thanks for any help
Upvotes: 0
Views: 1048
Reputation: 765
i do not answer quite exactly to your question, Although, that makes me think of customising the label of ytis. Example :
f(x)=x**2
f1(x)=sqrt(abs(x))
set ytics format ""
set ytics scale 1
set t qt 0 enhanced font "Sans,9"
#set mytics 8
# ploting once the function for getting the GPVAL_ variable
plot f(x) t "f(x)=x**2"
# calculating function for the list of increment
linRg(start,end,increment)=system(sprintf("seq %g %g %g", start, increment, end))
# forcing the tics, otherwise, the tics of the first plot will still be marked
set ytics GPVAL_Y_MIN, (GPVAL_Y_MAX-GPVAL_Y_MIN)/8, GPVAL_Y_MAX
do for [i in linRg(GPVAL_Y_MIN, GPVAL_Y_MAX, (GPVAL_Y_MAX-GPVAL_Y_MIN)/8)] { pr i; set ytics add ('f(|'.gprintf("%.1s%c",f1(i)).'|)'.gprintf("=%.1s%c",i) i)}
replot
Upvotes: 0
Reputation: 309929
You can use a for
loop. Of course, here you need to know your x-range in advance:
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)
Upvotes: 2
Reputation: 15910
Here is my semiautomatic answer, using a for loop to build the ytics string:
#!/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)
This is the result:
Upvotes: 0