如何在gnuplot中创建仅鼠标悬停时显示的超文本箭头?
在Gnuplot中实现鼠标悬停触发的超文本向量/箭头
已实现的点的超文本悬停效果
你已经完成了鼠标悬停在点上时显示提示文本的效果,对应的Gnuplot代码和数据文件如下:
Gnuplot代码
indices = "first second" # 绘制基础点 plot for [id in indices] "a_test.dat" index id u 1:2 # 添加超文本提示:鼠标悬停时显示点坐标 replot for [i=1:words(indices)] "a_test.dat" index word( indices, i ) u 1:2:(sprintf("point = (%d, %d)", column(1), column(2))) w labels hypertext point pt 7 ps 2 lc rgb '#ff000000' notitle
数据文件a_test.dat内容
# first x1 y1 5 1 6 2 9 8 # second x2 y2 4 5 8 2 2 7
实现向量/箭头的鼠标悬停触发显示
要实现仅在鼠标悬停在起点或终点附近时才显示对应向量/箭头,可以将hypertext与with vectors结合,通过隐藏触发点的方式达成需求,具体方案如下:
1. 准备向量数据
你可以在现有a_test.dat中新增向量段,或单独创建vectors.dat文件,记录每个向量的起点和终点坐标(示例中把first的点作为起点,second的点对应作为终点):
# vectors start_x start_y end_x end_y 5 1 4 5 6 2 8 2 9 8 2 7
2. Gnuplot实现代码
# 第一步:绘制基础的起点和终点(和原有逻辑一致) indices = "first second" plot for [id in indices] "a_test.dat" index id u 1:2 pt 7 ps 2 lc rgb 'blue' title id # 第二步:添加悬停触发的向量,仅鼠标靠近起点/终点时显示 # 针对起点的触发:悬停起点时显示向量 replot for [i=1:3] "vectors.dat" index "vectors" u 1:2:($3-$1):($4-$2):(sprintf("向量:从(%.1f,%.1f)到(%.1f,%.1f)", $1,$2,$3,$4)) \ with vectors hypertext point pt 0 ps 0 lc rgb 'red' notitle, \ # 针对终点的触发:悬停终点时显示向量 '' u 3:4:($1-$3):($2-$4):(sprintf("向量:从(%.1f,%.1f)到(%.1f,%.1f)", $1,$2,$3,$4)) \ with vectors hypertext point pt 0 ps 0 lc rgb 'red' notitle
代码说明
with vectors用于绘制向量,格式为u x:y:dx:dy,其中x,y是起点坐标,dx,dy是向量的x/y分量(终点坐标减起点坐标)。hypertext point pt 0 ps 0是核心设置:把触发悬停的点设为不可见(pt 0表示无点符号,ps 0表示点大小为0),不会在图上额外显示冗余点,仅当鼠标靠近对应起点/终点时,才会触发向量和提示文本的显示。- 通过
sprintf自定义提示文本,清晰展示向量的起点和终点坐标信息。
内容的提问来源于stack exchange,提问作者magfan
相关产品推荐
相关产品推荐

