在 GNUPLOT 中将鼠标悬停在函数上时显示 x 和 y 值的标签,以及可能的其他值

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

有没有一种方法,当我将鼠标悬停在一个函数上时,比如 f(x) = x**2,当我将鼠标移到函数上时,它会弹出一个标签,比如 x=2,它显示"x = 2 \n (new line) y = 4" 之类的标签?另外,如果可能的话,我可以让它包含外部值吗?我的意思是,如果我有一个梯度公式,对于 x**22*x,我可以告诉 GNUPLOT 在 x 和 y 标签值下方说“梯度:4”。它不一定是导数,但这只是我的意思的一个例子。

我猜你正在寻找 hypertext。在 gnuplot 控制台检查 help hypertext.

如果将鼠标指针移近数据点,将弹出一些文本。如果您使用更大的点数绘制标签,将更容易“捕捉”该点并显示超文本,例如ps 3 和“不可见”,即 lt -2 是背景颜色。 这适用于交互式 wxt 终端。您需要检查其他终端。使用 gnuplot 5.2.6 测试。

代码:

### 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",,)) w labels hypertext point pt 7 ps 3 lt -2 notitle, \
        '' u 1:2 w lp pt 7 title "f(x)"

### end of code

结果:(截图)

加法:

具有更多点并绘制的示例 with lines。没有捕捉到曲线,你必须沿着曲线移动。

代码:

### add hypertext (wxt terminal)
reset session

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

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

结果:(屏幕截图)

Addition2:(使用交互式放大功能)

代码:

### 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