Idea from @Jasper Habicht on this question: Plotting with LaTeX
How can one plot a function, so that the ends of the lines are automatic dotted like in this picture (green and blue functions):
Idea from @Jasper Habicht on this question: Plotting with LaTeX
How can one plot a function, so that the ends of the lines are automatic dotted like in this picture (green and blue functions):
\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}
This solution does unfortunately not work with the smooth option.
Edit: now with fixed length(0.5cm) dotted section to better work with different length plots:
\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}
pre length, so that the dotted part always has the same length if multiple plots are drawn.
– Jasper Habicht
Dec 20 '22 at 08:02
+ works?
– hpekristiansen
Dec 20 '22 at 11:32