![带有虚线端点的 PGFplots 中的绘图函数](https://linux22.com/image/458947/%E5%B8%A6%E6%9C%89%E8%99%9A%E7%BA%BF%E7%AB%AF%E7%82%B9%E7%9A%84%20PGFplots%20%E4%B8%AD%E7%9A%84%E7%BB%98%E5%9B%BE%E5%87%BD%E6%95%B0.png)
答案1
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\tikzset{
dotted ends/.style={
draw=none,
postaction={
draw, decoration={lineto,
pre=moveto, pre length=0.05*\pgfmetadecoratedpathlength,
post=moveto, post length=0.05*\pgfmetadecoratedpathlength,
}, decorate},
postaction={
draw, dotted, decoration={lineto,
post=moveto, post length=0.95*\pgfmetadecoratedpathlength,
}, decorate},
postaction={
draw, dotted, decoration={lineto,
pre=moveto, pre length=0.95*\pgfmetadecoratedpathlength,
}, decorate},
}}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[dotted ends, thick, blue, samples=50] {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
遗憾的是,此解决方案不适用于该smooth
选项。
编辑:现在使用固定长度(0.5 厘米)的虚线部分,可以更好地处理不同长度的图:
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\tikzset{
dotted ends/.style={
draw=none,
postaction={
draw, decoration={lineto,
pre=moveto, pre length=0.5cm,
post=moveto, post length=0.5cm,
}, decorate},
postaction={
draw, dotted, decoration={lineto,
post=moveto, post length=(\pgfmetadecoratedpathlength-0.5cm),
}, decorate},
postaction={
draw, dotted, decoration={lineto,
pre=moveto, pre length=(\pgfmetadecoratedpathlength-0.5cm),
}, decorate},
}}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[dotted ends, thick, cyan, samples=500] {10+10*sin(500*x)};
\addplot[dotted ends, thick, blue, samples=50] {x^2};
\end{axis}
\end{tikzpicture}
\end{document}