我可以使用 pgfplots 生成不同颜色的抛物线,但是我无法在图表上画箭头。
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,
xmin=-2,xmax=5,ymin=-15,ymax=15,
xlabel=$x$,
ylabel={$y$},
xtick distance=1
]
\addplot[smooth,color=blue,] {x^2-x+2};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
这是一项提议。箭头是通过 来添加的decorations.markings
,而不是使用已弃用的arrows
库。除其他事项外,还确保无需使用来切换到轴坐标。arrows.meta
\pgfplotsset{compat=1.16}
axis cs
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{arrows.meta,decorations.markings}
\begin{document}
\begin{tikzpicture}[add arrow/.style 2 args={postaction=decorate,decoration={%
markings,mark=at position #1 with {\arrow{#2}}}}]
\begin{axis}[axis lines=middle,width=6cm,height=8cm,
xmin=-0.5,xmax=5,ymin=0,ymax=10,domain=-0.5:4,
xlabel=$x$,
ylabel={$y$},
xtick distance=1
]
\addplot[smooth,color=blue,add arrow={0.25}{Stealth[purple,length=5pt]},
add arrow={0.36}{Stealth[reversed,purple,length=5pt]}] {x^2-x+2};
\draw[yellow!70!black,dashed] (0,4) -| (2,0)
node[pos=0.1,black,fill=white](4){4}
node[pos=0.9,black,fill=white](2){2};
\end{axis}
\draw[red,latex-] (4) edge ++ (0,0.5) edge ++ (0,-0.5)
(2) edge ++ (0.5,0) edge ++ (-0.5,0);
\end{tikzpicture}
\end{document}