我希望图表末端有箭头,但图例只显示直线(没有箭头)。
梅威瑟:
\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}
由此可知。
但是,我希望图例看起来像这样。
有没有办法让我制作不带箭头的图例?谢谢。
答案1
一个粗鲁的解决方案:
我将函数的两端与主要部分分开,并将图例与之关联:
\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}
完成:
更好更简洁的解决方案是,正如@percusse在他的评论中所建议的那样,使用 axis 选项legend image post style=-
。 代码如下:
\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}
得到的图片和上面一样。