Show a label of x and y values, and possibly other values when hovering over a function with the mouse in GNUPLOT

Viewed 142

Is there a way that when i hover over a function, say f(x) = x**2, that it pops up a label when i move my mouse onto the function at, say, x=2, it shows a label of "x = 2 \n (new line) y = 4" or something like that? Also if that is possible can I make it so that it includes external values? What i mean by this is if i have a gradient formula, for x**2, 2*x, I can tell GNUPLOT to say below the x and y label values, "Gradient: 4". It doesn't have to be the derivative but that is just an example of what i mean.

1 Answers

I guess you are looking for hypertext. In the gnuplot console check help hypertext.

If you move the mouse pointer close to a datapoint, some text will pop-up. It will be easier to "catch" the point and get the hypertext shown if you plot the labels with a pointsize a bit larger, e.g. ps 3 and "invisible", i.e. lt -2 which is the background color. This works with an interactive wxt terminal. You need to check other terminals. Tested with gnuplot 5.2.6.

Code:

### add hypertext (wxt terminal)
reset session

# create some test data
set print $Data
do for [x=-5:5] {
    print sprintf("%g %g", x, x**2)
}
set print

plot $Data u 1:2:(sprintf("x=%g\ny=%g",$1,$2)) w labels hypertext point pt 7 ps 3 lt -2 notitle, \
        '' u 1:2 w lp pt 7 title "f(x)"

### end of code

Result: (screenshot)

enter image description here

Addition:

Example with more points and plotted with lines. There is no snapping to the curve, you have to move along the curve.

Code:

### add hypertext (wxt terminal)
reset session

# create some test data
set samples 201
set xrange[-5:5]
set table $Data
    plot '+' u 1:($1**2):(2*$1) w table
unset table

set xlabel 'x'
set ylabel 'y'
plot $Data u 1:2:(sprintf("x=%.2f\ny=%.2f\ndy/dx=%.2f",$1,$2,$3)) w labels hypertext point pt 7 ps 1 lt -2 notitle, \
        '' u 1:2 w l lw 2 title "f(x)"
### end of code

Result: (screen capture)

enter image description here

Addition2: (using functions for interactive zoom-in)

Code:

### add hypertext (wxt terminal) with functions for interactive zoom-in
reset session

set xlabel 'x'
set xrange[-5:5]
set ylabel 'y'
set samples 201

f(x) = x**2
g(x) = 2*x

plot '+' u (x):(f(x)):(sprintf("x=%.2g\ny=%.2g\ndy/dx=%.2g",x,f(x),g(x))) w labels hypertext point pt 7 ps 1 lt -2 notitle, \
        f(x) w l lw 2 title "f(x)"
### end of code
Related