在 Gnuplot 中,我可以突出显示线性图上的特定值吗?通过突出显示“,我的意思是从轴值到图表上的特定点绘制线条,并用点打印该点。我有一些这样有趣的值。我可以使用“设置箭头”等手动“绘制”所有这些,但我想知道是否有内置机制。
我的数据来自文件,而不是数学函数。有没有办法得到 给定 x 的 y 值,以便“突出显示”会自动 检索给定的 x?
Gnuplot 版本 6 将支持这一点。它已经在开发分支中存在了一段时间了。如果您愿意从源代码构建或尝试测试构建,可以从 SourceForge 上的 gnuplot 主站点获取 6.0 的候选版本。
完整文档和示例图位于此处: http://gnuplot.info/docs_6.0/loc4436.html
简短示例:
plot DATA using 1:2 smooth cnormal watch y=0.25 watch y=0.5 watch y=0.75
EDIT2:正如@Ethan回答,从6.0版本开始将会有一个内置的解决方案。在那之前,如果你有一个函数 f(x) [左图]:
,就有一个手工解决方案# gnuplot 5.4.4
reset; set grid;
xmin=0
ymin=0
set xrange [xmin:]
set yrange [ymin:]
highlight(x, y, xmin, ymin) \
= sprintf("set arrow from %f,%f to %f,%f nohead lw 2 lc -1; \
set arrow from %f,%f to %f,%f nohead lw 2 lc -1; \
set label at %f,%f point pt 7 lc -1 ps 2; ", xmin, y, x, y, x, ymin, x, y, x, y)
f(x)=0.5*x+0.2
eval highlight(4,2.2,xmin,ymin)
eval highlight(6,f(6),xmin,ymin)
plot f(x) notitle
编辑:由于OP要求使用文件中的数据点而不是函数的解决方案,因此有一个可能的解决方案[右图]:
# gnuplot 5.4.4
reset; set colors classic; set grid
datafile='007_d.dat'
xmin=0
ymin=0
set xrange [xmin:]
set yrange [ymin:]
highlight(x, y, xmin, ymin) \
= sprintf("set arrow from %f,%f to %f,%f nohead lw 2 lc -1; \
set arrow from %f,%f to %f,%f nohead lw 2 lc -1; \
set label at %f,%f point pt 7 lc -1 ps 2; ", xmin, y, x, y, x, ymin, x, y, x, y)
byx(x) \
= sprintf("plot datafile u ($1 == %f ?$1:1/0):2; \
eval highlight(GPVAL_DATA_X_MIN,GPVAL_DATA_Y_MIN,xmin,ymin)", x)
byy(y) \
= sprintf("plot datafile u ($2==2.7?$1:1/0):2; \
eval highlight(GPVAL_DATA_X_MIN,GPVAL_DATA_Y_MIN,xmin,ymin)", y)
eval highlight(4,2.2,xmin,ymin)
eval byx(6)
eval byy(2.7)
plot datafile w lp ps 2 notitle
注意: 当然,您可以使用 datafile 作为 byx() / byy() 的额外参数;您也可以根据您的特殊需求使用 xmin amd ymin。
注意:代码假设您有唯一的 x-y 数据对。
这是一个不同的解决方案:
如果你没有函数,只有数据文件,则需要修改脚本。
脚本:
### mark special points on a function
reset session
f(x) = x**2 + 4*x + 1
$Marks <<EOD
-5
7
EOD
do for [i=1:|$Marks|] {
set obj i rect from graph -1, graph -1 to first $Marks[i], first f($Marks[i]) fs empty
set label i at first $Marks[i], first f($Marks[i]) sprintf("(%g,%g)",real($Marks[i]), f($Marks[i])) point pt 7 lc "red" right offset 0,1
}
plot f(x) w l lc "blue"
### end of script
结果:
我认为您可以通过将 xmin 替换为 GPVAL_X_MIN 并将 ymin 替换为 GPVAL_Y_MIN 来简化此操作。
这些是内置变量。
reset
set grid
highlight(x, y) \
= sprintf("set arrow from %f,%f to %f,%f nohead lw 2 lc -1; \
set arrow from %f,%f to %f,%f nohead lw 2 lc -1; \
set label at %f,%f point pt 7 lc -1 ps 2; ", GPVAL_X_MIN, y, x, y, x, GPVAL_Y_MIN, x, y, x, y)
f(x)=0.5*x+0.2
eval highlight(4,2.2)
eval highlight(6,f(6))
plot f(x) notitle
replot
重绘之所以存在,是因为只有绘制一次后才会设置内置变量。