Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am using tikz to generate sine and cosine graphs with arrows at the ends of each curve. The arrows appear fine for the sine curve, but not so for the cosine curve. Why does this occur and how do I fix it?

Here is my code:

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{figure}[h]
    \centering
        \begin{tikzpicture}[scale=1,>=latex,x=0.5cm,y=2.0cm]
            \draw[<->,thick,domain=-4*pi:4*pi,samples=250,color=red] plot (\x,{sin(\x r)}) node[below right] {\footnotesize \textcolor{red}{$y= \sin(x)$}};
            \draw[<->,thick,domain=-4*pi:4*pi,samples=250,color=blue] plot (\x,{cos(\x r)}) node[right] {\footnotesize \textcolor{blue}{$y= \cos(x)$}};
            \draw[->,thick] (-14,0) -- (14,0) node[below left]{\footnotesize $x$};
            \draw[->,thick] (0,-2) -- (0,2) node[below right]{\footnotesize $y$};
        \end{tikzpicture}
    \caption{Graphs of sine and cosine}
\end{figure}
\end{document}
share|improve this question
    
It occurs because the plot is made up of many (c. 250) short curves segments and the arrow tip is only applied to the last curve segment. For the sine curve the final segments point in roughly the same direction, whereas for cosine they are varying rapidly. A crude fix is to draw the arrows separately. –  Andrew Swann Jul 18 at 7:42
    
a nicer fix is to do domain=-3.9*pi:3.9*pi for the cosine –  LaRiFaRi Jul 18 at 7:44

2 Answers 2

up vote 7 down vote accepted

When PGF puts an arrowhead on a line it "backs-up" along the last path segment so that the end of the arrow tip is at the end of path. As plots are (usually) lots of small straight lines PGF backs up along the last straight line segment.

When the gradient of the line segments are changing relatively slowly (as in the end points of the sine curve in the MWE) it looks OK. Otherwise (i.e., the end points of the cosine curve in the MWE) it does not.

Although PGF 3.0 adds lots of fancy features for bending and flexing arrows, I think the simplest solution is to lengthen the first and final line-segments. Fortunately this is easy by using the shorten > and shorten < keys with negative values.

\documentclass[border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,bending}
\begin{document} 
        \begin{tikzpicture}[scale=1,>=latex,x=0.5cm,y=2.0cm]
            \draw[<->,thick,domain=-4*pi:4*pi,samples=250,color=red] plot (\x,{sin(\x r)}) node[below right] {\footnotesize \textcolor{red}{$y= \sin(x)$}};
            \draw[<->,thick,domain=-4*pi:4*pi,samples=250,color=blue,
shorten >=-4pt, shorten <=-4pt] 
plot (\x,{cos(\x r)}) node[right=5pt] {\footnotesize \textcolor{blue}{$y= \cos(x)$}};
            \draw[->,thick] (-14,0) -- (14,0) node[above left]{\footnotesize $x$};
            \draw[->,thick] (0,-2) -- (0,2) node[below right]{\footnotesize $y$};
        \end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
Nice explanation and solution, thanks! –  DJJerome Jul 18 at 16:00

The problem is that the arrow head in TikZ is not appended to the line but super-positioned over its end. As the curvature at the end of you cosine curve is quite big, this results in your ugly result (the direction of the arrow is correct...).

I added to possible hacks you could try. The first is just shortening the graph to a section with lower curvature (green line) and the other plots an invisible graph for y=1 with blue arrow heads.

% arara: pdflatex

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{figure}
\centering
    \begin{tikzpicture}[scale=1,>=latex,x=0.5cm,y=2.0cm]
        \draw[<->,thick,domain=-4*pi:4*pi,samples=250,color=red] plot (\x,{sin(\x r)}) node[above right] {\footnotesize $y= \sin(x)$};
        \draw[-,thick,domain=-4*pi:4*pi,samples=250,color=blue] plot (\x,{cos(\x r)}) node[right] {\footnotesize $y= \cos(x)$};
        \draw[<->,>={LaTeX[]},draw opacity=0, domain=-4.1*pi:4.1*pi, color=blue] plot (\x, 1);
        \draw[<->,thick,domain=-3.9*pi:3.9*pi,samples=250,color=green] plot (\x,-{cos(\x r)}) node[right] {\footnotesize $y= -\cos(x)$};
        \draw[->,thick] (-14,0) -- (14,0) node[below left]{\footnotesize $x$};
        \draw[->,thick] (0,-2) -- (0,2) node[below right]{\footnotesize $y$};
    \end{tikzpicture}
    \caption{Graphs of sine and cosine}
\end{figure}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.