3

I want to have the arrows on the ends of the graph but have the legend just show straight lines (without arrows).

MWE:

\documentclass[12pt]{article}

\usepackage{amsmath, amssymb}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}

\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usetikzlibrary{positioning,calc}

\begin{document}

\begin{center}
\begin{tikzpicture}

\begin{axis}[grid=both,
 axis lines=middle,
 ticklabel style={fill=white},
 xmin=-3.5,xmax=3.5,xtick={-3,-2,-1,1,2,3},
 ymin=-1.5,ymax=4.5,ytick={-1,0,1,2,3,4},
 xlabel=\(x\),ylabel=\(y\),
 samples=200,
 legend style={at={(axis cs:2.5,1.5)},anchor=south west},legend cell align=left]

\addplot[domain=-2:2,blue,<->,>=latex] {x^2};
\addlegendentry{\text{ }\(y=x^2\)}
\addplot[domain=-pi:pi,red,<->,>=latex] {cos(deg(x))};
\addlegendentry{\text{ }\(y=\cos x\)}

\end{axis}

\end{tikzpicture}
\end{center}


\end{document}

Which yields this.

However, I want the legend to look like this.

Is there a way for me to produce the legend without the arrows? Thanks.

1 Answer 1

4

An rude solution: enter image description here

I separate ends of function from main part to which I tied a legend:

\documentclass[12pt]{article}
    \usepackage{amsmath, amssymb}
    \usepackage{pgfplots}
\usetikzlibrary{positioning,calc}

    \begin{document}
\begin{tikzpicture}[>=latex]
    \begin{axis}[grid=both,
 axis lines=middle,
 ticklabel style={fill=white},
 xmin=-3.5,xmax=3.5,xtick={-3,-2,-1,1,2,3},
 ymin=-1.5,ymax=4.5,ytick={-1,0,1,2,3,4},
 xlabel=\(x\),ylabel=\(y\),
 samples=200,
 legend style={at={(axis cs:2.5,1.5)},anchor=south west},legend cell align=left]
\addplot[domain=-1.9:1.9,blue]   {x^2};
\addlegendentry{\ $y=x^2$}
\addplot[domain=-0.9*pi:0.9*pi,red]     {cos(deg(x))};
\addlegendentry{\ $y=\cos x$}

    \addplot[domain=-1.9:-2,blue,->] {x^2};
    \addplot[domain= 1.9: 2,blue,->] {x^2};

    \addplot[domain=-0.9*pi:-pi,red,->] {cos(deg(x))};
    \addplot[domain= 0.9*pi: pi,red,->] {cos(deg(x))};
    \end{axis}
\end{tikzpicture}
    \end{document}

Completion:

Far better and concise solution is, as suggested @percusse in his comment, to use axis option legend image post style=-. With it the code is:

\documentclass[12pt]{article}
    \usepackage{amsmath, amssymb}
    \usepackage{pgfplots}
\usetikzlibrary{positioning,calc}

    \begin{document}
\begin{tikzpicture}[>=latex]
    \begin{axis}[grid=both,
 axis lines=middle,
 ticklabel style={fill=white},
 xmin=-3.5,xmax=3.5,xtick={-3,-2,-1,1,2,3},
 ymin=-1.5,ymax=4.5,ytick={-1,0,1,2,3,4},
 xlabel=\(x\),ylabel=\(y\),
 samples=200,
 legend style={at={(axis cs:2.5,1.5)},
 legend image post style=-,% suggested by percusse
 anchor=south west},
 legend cell align=left]
\addplot[domain=-2:2,blue,<->]   {x^2};
\addlegendentry{\ $y=x^2$}
\addplot[domain=-pi:pi,red,<->]     {cos(deg(x))};
\addlegendentry{\ $y=\cos x$}
    \end{axis}
\end{tikzpicture}
    \end{document}

The obtained picture is the same as above.

2
  • @perccusse, thank you to point on this elegant and concise possibilities. I wasn't aware of it, so I declare my solution a rude what it realy is :-). I will add your suggestion to my answer. Thank you very much!
    – Zarko
    Commented Jun 3, 2015 at 22:04
  • No problem at all. Sometimes man gotta do what he gotta do. This time we are saved by the manual :)
    – percusse
    Commented Jun 3, 2015 at 22:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .