Gnuplot:如何在折线图中高亮特定数值?
Gnuplot高亮折线图特定数值的实现方案
Gnuplot没有专门的内置一键命令实现你要的“从坐标轴连线到特定点+标记圆点”的高亮效果,但可以通过数据分层处理或批量命令生成的方式,替代手动写set arrow的繁琐操作,效率更高。
方法1:分离高亮数据文件
把需要关注的点单独存入一个数据文件(比如highlight_points.dat),和主数据格式一致(每行是x y),然后分图层绘图:
# 绘制基础折线图 plot 'main_data.dat' with lines lw 2 lc rgb '#333333' title '主数据' # 叠加高亮元素:圆点+从x轴到点的连线 replot 'highlight_points.dat' \ with points pt 7 ps 1.5 lc rgb 'crimson' title '关注数据', \ '' using 1:0:1:2 with vectors nohead lc rgb 'crimson' dt 2 title ''
using 1:0:1:2指定向量起点为(x, 0)(x轴位置),终点为(x, y)(目标点)- 如果需要从y轴连线,改成
using 0:2:1:2即可
方法2:在主数据中添加标记列
在主数据文件里新增第三列,用1标记需要高亮的点,0标记普通点,通过筛选条件绘图:
# 绘制主折线 plot 'main_data.dat' using 1:2 with lines title '主数据' # 筛选标记为1的点,绘制高亮圆点和坐标轴连线 replot 'main_data.dat' using 1:2:(0):(0) with vectors nohead lc rgb 'darkblue' dt 1 title '' \ , '' using 1:2 with points pt 6 ps 1.2 lc rgb 'darkblue' title '关注数据'
- 这里
using 1:2:(0):(0)定义了向量的起点(x,0)和终点(x,y),自动对所有标记点生成连线
方法3:自动提取特定点批量生成箭头
如果要高亮极值点这类有规律的点,可以用stats命令提取坐标,再批量生成箭头和标记:
# 统计数据中的最大值点 stats 'main_data.dat' using 1:2 nooutput max_x = STATS_pos_max_y_x # 最大值对应的x坐标 max_y = STATS_max_y # 最大值对应的y坐标 # 生成十字连线和圆点标记 set arrow from max_x, graph 0 to max_x, max_y lc rgb 'orange' dt 1 set arrow from graph 0, max_y to max_x, max_y lc rgb 'orange' dt 1 set object circle at max_x, max_y size 0.02 fill solid lc rgb 'orange' # 绘制主折线 plot 'main_data.dat' with lines
这个方案适合自动识别关键点,无需手动指定坐标。
内容的提问来源于stack exchange,提问作者MrSparkly
相关产品推荐
相关产品推荐

