我想要绘制 $y = \sin^2(x) + \cos(x)$。
\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=4in,axis on top,clip=false,
axis lines=middle,
xmin=-6.2832,xmax=7.4259,
ymin=-1,ymax=1.25,
restrict y to domain=-1:1.25,
ticklabel style={font=\tiny},
xtick={-1.0472, 1.0472},
xticklabels={\makebox[0pt][r]{$\scriptstyle-$}$\scriptstyle\dfrac{\pi}{3}$, $\scriptstyle\dfrac{\pi}{3}$},
xlabel=$x$,ylabel=$y$,
ytick={-1, 1.25},
yticklabels={$\scriptstyle-1$, $\scriptstyle\dfrac{5}{4}$},
axis line style={latex-latex},
enlargelimits={abs=0.25cm},
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot[samples=201,domain=-6.2832:6.2832, dashed] {(sin(deg(x)))^2 + cos(deg(2*x))} ;
\end{axis}
\end{tikzpicture}
\end{document}
答案1
第 29 行的“2”太多了,应该是cos(deg(x))
:
\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=4in,axis on top,clip=false,
axis lines=middle,
xmin=-6.2832,xmax=7.4259,
ymin=-1,ymax=1.25,
restrict y to domain=-1:1.25,
ticklabel style={font=\tiny},
xtick={-1.0472, 1.0472},
xticklabels={\makebox[0pt][r]{$\scriptstyle-$}$\scriptstyle\dfrac{\pi}{3}$, $\scriptstyle\dfrac{\pi}{3}$},
xlabel=$x$,ylabel=$y$,
ytick={-1, 1.25},
yticklabels={$\scriptstyle-1$, $\scriptstyle\dfrac{5}{4}$},
axis line style={latex-latex},
enlargelimits={abs=0.25cm},
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot[samples=201,domain=-6.2832:6.2832, dashed] {(sin(deg(x)))^2 + cos(deg(x))} ;
\end{axis}
\end{tikzpicture}
\end{document}