You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gnuplot:制作含linespoints的图例且拟合线仅显示线条

解决拟合线显示与图例样式冲突的问题

核心思路

通过手动添加图例项的方式,让图中拟合线仅显示线条,同时图例呈现linespoints样式,避免直接用linespoints绘图导致拟合线被点覆盖的问题。

具体代码修改方案

假设你原本的基础代码如下:

\documentclass{article}
\usepackage{gnuplottex}
\begin{document}
\begin{gnuplot}[terminal=pdf]
set title "拟合结果"
set key top right
f(x) = a*x + b
fit f(x) 'data.txt' via a,b
plot 'data.txt' with points title "原始数据", \
     f(x) with linespoints title "拟合线"  # 此写法会导致拟合线出现点,不符合需求
\end{gnuplot}
\end{document}

修改为以下代码即可解决问题:

\documentclass{article}
\usepackage{gnuplottex}
\begin{document}
\begin{gnuplot}[terminal=pdf]
set title "拟合结果"
set key top right
# 定义拟合函数并完成拟合
f(x) = a*x + b
fit f(x) 'data.txt' via a,b

# 1. 绘制原始数据点,正常生成图例
plot 'data.txt' with points title "原始数据", \
     # 2. 绘制拟合线,仅显示线条,不自动生成图例(notitle)
     f(x) with lines notitle

# 3. 手动添加linespoints样式的图例项,对应拟合线
set key add '' f(x) with linespoints title "拟合线"
\end{gnuplot}
\end{document}

关键说明

  • 用with lines notitle绘制拟合线,确保图中仅显示线条,不会出现点覆盖的情况。
  • 通过set key add命令手动添加图例项,指定linespoints样式,让图例呈现你需要的效果。
  • 若要统一线条和点的样式,可额外设置linetype、pointtype参数,示例:
set key add '' f(x) with linespoints linetype 2 pointtype 6 title "拟合线"

替代兼容方案(拆分绘图层)

如果set key add在你的Gnuplot版本中存在兼容性问题,可通过空数据文件模拟图例项:

\begin{gnuplot}[terminal=pdf]
set title "拟合结果"
set key top right
f(x) = a*x + b
fit f(x) 'data.txt' via a,b

# 先绘制拟合线(仅线条)和原始数据
plot f(x) with lines linetype 2 notitle, \
     'data.txt' with points pointtype 6 title "原始数据"

# 用空数据文件绘制linespoints样式的图例项
plot '-' with linespoints linetype 2 pointtype 6 title "拟合线"
1 1  # 填写一个超出绘图范围的坐标,避免在图中显示
e
\end{gnuplot}

内容的提问来源于stack exchange,提问作者Konstantin Weigmann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 15:00:58