# PGF Plot Sampling Messes up Arrows

When creating some plots with pgf, sampling messes up arrow tips of some functions such as the square root function. I increase the sampling to 100 to make the function look better but the arrow at the end gets messed up. This topic has been brought up before here but I cannot find an answer to resolve this issue.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\usetikzlibrary{arrows.meta ,shapes,angles, decorations.pathmorphing}

\begin{tikzpicture}
\draw[line width=0.5pt, -{Latex[length=6pt,width=4pt]}] (-3.5,0)--(3.5,0)node[above, xshift=-0.12cm]{$x$};
\draw[line width=0.5pt, -{Latex[length=6pt,width=4pt]}] (0,-3.5)--(0,3.5)node[right, yshift=-0.15cm]{$y$};

\draw[xscale=1, yscale=1, line width=1pt, domain=-3.5:-0.6,
smooth,variable=\x,latex-latex,samples=200] plot ({\x},{(-2/\x)});
\draw[xscale=1, yscale=1, line width=1pt, domain=0.6:3.5,
smooth,variable=\x,latex-latex,samples=200] plot ({\x},{(-2/\x)});

\foreach \x/\y in {-1/2} {
\fill (\x,\y) circle (3pt);
\node[above left] at (\x,\y) {$(\x,\y)$};
}
\end{tikzpicture}
\end{document}


• It's going to be difficult to suggest corrections to a code you don't show... Please read about Minimal Working Example and edit your question accordingly. Thank you. Jul 28 at 18:09
• @Miyase I've now included and example.
– Jon
Jul 28 at 18:19
• Quick fix: shorten >=-2pt, shorten <=-2pt. Jul 28 at 18:25
• @Jasper Habicht Thanks, this works; I hope there is a more standard way this can be dealt with.
– Jon
Jul 28 at 20:28

This is not possible to do without cheating. The plot draws many small line segments (or many small curves with the smooth option). When an arrow tip is applied, the curve is shortened to not show the curve under the tip. A shortening is only applied to the first(or last) line segment(or curve), so when it is too short it fails.

From the manual about arrows page 192:

TikZ will modify the path by shortening the first segment and shortening a segment below its length may result in strange effects.

The cheating solution could be to cover the line with something white (if the background is not white or monochromatic, the solution gets a bit more complicated) and then draw the arrow afterwards like this:

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\draw[ultra thick, {Circle[white]}-{Circle[white]}, postaction={Latex-Latex, tips}] plot[domain=0.6:3.5, samples=200] ({\x},{(-2/\x)});
\end{tikzpicture}
\end{document}


A color version, to show what is happening

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\draw[ultra thick, {Circle[blue]}-{Circle[blue]}, postaction={{Latex[teal]}-{Latex[teal]}, tips}] plot [domain=0.6:3.5, samples=200] ({\x},{(-2/\x)});
\draw[red] plot[domain=0.6:3.5, samples=200] ({\x},{(-2/\x)});
\end{tikzpicture}
\end{document}


• @JasperHabicht: With your negative shortening, you make the first and last segment long enough that it is no longer a problem for the arrow to do its shortening. With this solution, the arrowhead ends up in the wrong place. Jul 31 at 20:06
• Thank you for the in-depth explanation. I appreciate the knowledge you have shared.
– Jon
Jul 31 at 22:21