![将刻度标记定位在 pi/2 的整数倍处](https://linux22.com/image/313188/%E5%B0%86%E5%88%BB%E5%BA%A6%E6%A0%87%E8%AE%B0%E5%AE%9A%E4%BD%8D%E5%9C%A8%20pi%2F2%20%E7%9A%84%E6%95%B4%E6%95%B0%E5%80%8D%E5%A4%84.png)
在我的代码中,我原本有
xtick={-2*pi, -(3/2)*pi, -(1/2)*pi, (1/2)*pi,(3/2)*pi, 2*pi},
在 $pi/2$ 的整数倍处放置刻度标记。它无法编译。我不得不用繁琐的十进制近似值代替它。为什么代码无法使用像 这样的表达式进行编译pi
?为了指定环境中 x 轴的宽度axis
,我使用了xmin=-2*pi,
和xmax=2*pi,
,为了指定余弦函数的定义域,我使用了domain=-2*pi:2*pi
。
\documentclass{amsart}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings,backgrounds,patterns}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=4.5in,clip=false,
xmin=-2*pi,xmax=2*pi,
ymin=-1.25,ymax=1.25,
axis lines=middle,
%xtick={-2*pi, -(3/2)*pi, -pi, -(1/2)*pi, (1/2)*pi, pi, (3/2)*pi, 2*pi},
xtick={-6.28318, -4.7123889, -3.14159, -1.5708, 1.5708, 3.14159, 4.7123889, 6.28318},
xticklabel style={font=\footnotesize,fill=white},
xticklabels={$-2\pi$,,$-\pi$,,,$\pi$,,$2\pi$},
ytick={-1},
yticklabel style={font=\footnotesize,fill=white},
yticklabels={$-1$},
extra y ticks={1},
extra y tick labels={$1$},
extra y tick style={yticklabel style={font=\footnotesize,fill=white,anchor=south east}},
ticklabel style={font=\footnotesize,fill=white},
axis line style={latex-latex},
axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot [samples=501,mark=none, thick, red,domain=-2*pi:2*pi] {cos(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
随着 PGFPlots v1.16 的发布,现在可以在多个地方使用数学解析器。因此,您xtick
现在可以使用提供的行...
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xtick={-2*pi, -(3/2)*pi, -pi, -(1/2)*pi, (1/2)*pi, pi, (3/2)*pi, 2*pi},
% xtick={-6.28318, -4.7123889, -3.14159, -1.5708, 1.5708, 3.14159, 4.7123889, 6.28318},
xticklabel style={font=\footnotesize,fill=white},
xticklabels={$-2\pi$,,$-\pi$,,,$\pi$,,$2\pi$},
ytick={-1,1},
axis lines=middle,
domain=-2*pi:2*pi,
samples=501,
enlargelimits=true,
]
\addplot [thick,red] {cos(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
\pgfmathparse
可以通过或单独评估列表\pgfmathsetmacro
。逗号列表将转换为数组,其中计算出的数字放在花括号中。因此,以下示例定义宏\ArrayToCommaList
以再次获取逗号分隔的列表:
\documentclass{amsart}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings,backgrounds,patterns}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\makeatletter
\newcommand*{\ArrayToCommaList}[1]{%
\let\ArrayToCommaList@Result\@empty
\expandafter\@tfor\expandafter\ArrayToCommaList@Entry
\expandafter:\expandafter=#1\do{%
\edef\ArrayToCommaList@Result{%
\ArrayToCommaList@Result,\ArrayToCommaList@Entry
}%
}%
% Remove first comma
\edef#1{%
\expandafter\@cdr\ArrayToCommaList@Result\@empty\@nil
}%
}
\makeatother
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro\xticklist{-2*pi, -(3/2)*pi, -pi, -(1/2)*pi,
(1/2)*pi, pi, (3/2)*pi, 2*pi}
\ArrayToCommaList\xticklist
\begin{axis}[width=4.5in,clip=false,
xmin=-2*pi,xmax=2*pi,
ymin=-1.25,ymax=1.25,
axis lines=middle,
xtick=\xticklist,
xticklabel style={font=\footnotesize,fill=white},
xticklabels={$-2\pi$,,$-\pi$,,,$\pi$,,$2\pi$},
minor x tick num=2,
ytick={-1},
yticklabel style={font=\footnotesize,fill=white},
yticklabels={$-1$},
extra y ticks={1},
extra y tick labels={$1$},
extra y tick style={yticklabel style={font=\footnotesize,fill=white,anchor=south east}},
ticklabel style={font=\footnotesize,fill=white},
axis line style={latex-latex},
axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot [samples=501,mark=none, thick, red,domain=-2*pi:2*pi] {cos(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}
答案3
运行xelatex
\documentclass{amsart}
\usepackage{pst-plot}
\begin{document}
\psset{yunit=3}
\begin{pspicture}(-6.75,-1.3)(6.75,1.3)
\psframe*[linecolor=yellow!80!black!20](-6.7,-1.3)(6.7,1.3)
\psaxes[trigLabels,dx=3.14]{<->}(0,0)(-6.6,-1.25)(6.6,1.25)
\psplot[algebraic,linewidth=1.5pt,
linecolor=red,plotpoints=1000]{-6.28}{6.28}{cos(x)}%
\end{pspicture}
\end{document}
每 pi/2 刻一次:
\psaxes[trigLabels,trigLabelBase=2,dx=1.57]{<->}(0,0)(-6.6,-1.25)(6.6,1.25)
答案4
\documentclass[12pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\usepackage{mathrsfs}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]
\draw[->,color=black] (-4.3,0.) -- (5.1,0.);
\draw[shift={(-3.14,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $-\pi$};
\draw[shift={(-1.6,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $-\pi/2$};
\draw[shift={(1.6,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\pi/2$};
\draw[shift={(3.14,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\pi$};
\draw[shift={(4.7,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $3\pi/2$};
\draw[->,color=black] (0.,-2.11) -- (0.,1.33);
\foreach \y in {-2.,-1.,1.}
\draw[shift={(0,\y)},color=black] (.5pt,0pt) -- (-.5pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
\draw[line width=2.pt,color=magenta,smooth,samples=500,domain=-5:5] plot(\x,{cos(((\x))*180/pi)});
\begin{scriptsize}
\draw[color=magenta] (-4.6,.5) node {$f(x)=\cos x$};
\end{scriptsize}
\end{tikzpicture}
\end{document}