我想在 tikz 图片上以 π/4 为增量标记 x 轴,就像在维基百科 sin 和 cos 图形中一样1。这是我的代码(但它不起作用):
\documentclass{scrartcl}
\usepackage{lmodern} % Schriftart
\usepackage[utf8]{inputenc} % Umlaute im Quellcode benutzen
\usepackage[T1]{fontenc} % Umlaute korrekt darstellen
\usepackage[ngerman]{babel} % Worttrennung
\usepackage[landscape, margin=20mm]{geometry}
\usepackage{blindtext}
\usepackage{tikz}
\pagestyle{empty}
\setlength\parindent{0mm}
\begin{document}
\blindtext
\begin{tikzpicture}[scale=2, domain=0:{2*pi}]
%\clip (-0.1,-0.2) rectangle (1.1,0.75); %Bildausschnitt
\draw[step=0.5,gray,very thin] (0,-1.4) grid (6.4,1.4); %Hintergrundgitter
\draw[->] (0,0) -- (6.5,0) node[right]{$x$}; %x-Achse
\draw[->] (0,-1.5) -- (0,1.5) node[above]{$y$}; %y-Achse
\foreach \x in {0\pi,0.5\pi,...\pi,2\pi}{$\x$}
\draw (\x,2pt) -- (\x,-2pt) node[below]{$\x$};
\foreach \y in {-1,-0.5, ...,1}
\draw(2pt,\y) -- (-2pt,\y) node[left]{$\y$};
\draw[color=red, very thick, smooth] plot (\x,{sin(\x r)});
\draw[color=blue, very thick, smooth] plot (\x,{cos(\x r)});
%\draw[smooth, variable=\x, blue] (0.0) plot ({\x}, {sin(\x r)});
\end{tikzpicture}
\end{document}
答案1
如果你真的想坚持tikz
(虽然我建议你使用pgfplots
它来绘图),你可以:
- 用来
\pgfmathsetmacro
计算适当的 x 轴位置作为 的倍数pi
。 - 用于
\pgfmathprintnumber
格式化数字,数字格式设置为frac
。 - 检测 的情况
1
(这样您就不会得到 的标签1\pi
)。
笔记:
\pi
不是数字。它用于排版希腊字母 pi。- 有关如何使用的一个示例
pgfplots
可在PGFPlots 中带有三角标签的轴。
参考:
- 如果您要创建多个图表,我建议您切换到使用专门为图表设计的包
axis
中的环境pgfplots
。其与三角函数图一起使用的示例如下PGFPlots 中带有三角标签的轴。
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fpu}% needed for for the 'frac' detection
\NewDocumentCommand{\FormatMultipleOfGivenValue}{s O{} m}{%
%% #1 = * to suppress the display of "0"
%% #2 = multiplier to display
%% #3 = real number
\pgfmathifisint{#3}{%
\pgfmathparse{int(#3)}%
\ifnum\pgfmathresult=0
\IfBooleanF{#1}{0}% Suppress "0" if * option provided.
\else
\ifnum\pgfmathresult=1\relax
%% https://tex.stackexchange.com/q/53068
\ifx#2\pi\relax\pi\else1\fi
\else
\pgfmathresult#2
\fi
\fi
}{%
\pgfmathprintnumber{#3}#2
}%
}%
\begin{document}
\begin{tikzpicture}[scale=2, domain=0:{2*pi}]
\pgfkeys{/pgf/number format/.cd, frac, frac whole=false}
\draw [xstep=0.25*pi, ystep=0.5, gray,very thin] (0,-1.0) grid (2*pi,1.0);
\draw [-latex] (0,0) -- (6.5,0) node [right] {$x$}; %x-Achse
\draw [-latex] (0,-1.3) -- (0,1.3) node [above] {$y$}; %y-Achse
\foreach \x in {0.0, 0.5,..., 2, 6.5}{
\pgfmathsetmacro\xCoord{\x*pi}
\draw (\xCoord,2pt) -- (\xCoord,-2pt) node [below]
{$\FormatMultipleOfGivenValue*[\pi]{\x}$};
}
\foreach \y in {-1,-0.5, ...,1} {
\draw (2pt,\y) -- (-2pt,\y) node [left] {$\FormatMultipleOfGivenValue{\y}$};
}
\draw[color=red, ultra thick, smooth] plot (\x,{sin(\x r)});
\draw[color=blue, ultra thick, smooth] plot (\x,{cos(\x r)});
\end{tikzpicture}
\end{document}
答案2
试试这个代码:
\documentclass[margin=.5cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[>=latex,scale=1.5]
% first option for a grid like a millimetral grahic paper (next 3 lines)
\draw[thin,brown!25] (-.5,-1.5) grid[step= 1mm] ++ (7.5,3);
\draw[semithick,brown!50] (-.5,-1.5) grid[step= 5mm] ++ (7.5,3);
\draw[ thick,brown!75] (-.5,-1.5) grid[step=10mm] ++ (7.5,3);
% second option for a grid in dotted gray in next line (uncomment the next line but comment the preceding 3 lines)
%\draw[dotted] (-0,-1) grid (6.5,1);
% x axis and its label
\draw[->] (-.5,0) -- (6.5,0) node[right] {$x$};
% x axis graduations in integers
\foreach \x in {1,2,...,6}
\fill[shift={(\x,0)}] circle(.5pt) node[above] {\tiny $\x$};
% x axis graduations in \pi
\foreach \x /\n in {.7854/$\frac{\pi}{4}$,1.5708/$\frac{\pi}{2}$,2.3562/$\frac34\pi$,3.1416/$\pi$,3.9270/$\frac54\pi$,4.7124/$\frac32\pi$,5.4978/$\frac74\pi$,6.2832/$2\pi$} \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {\tiny \n};
% y axis, its label and graduations
\draw[->] (0,-1.5) -- (0,1.5) node[below right] {$y$};
\foreach \y in {-1,1}
\draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {\tiny $\y$};
\node[below left] at (0,0) {\tiny $0$};
% draw functions
\draw[smooth,blue,mark=none,domain=0:6.283,line width=1.5pt] plot (\x,{sin(deg(\x))});
\draw[smooth,magenta,mark=none,domain=-0:6.283,line width=1.5pt] plot (\x,{cos(deg(\x))});
% draw the functions names
\draw[] (2,-1) node[magenta,below](1) {$y=\cos x$};
\draw[] (2,1) node[blue,above](1) {$y=\sin x$};
% plot the intersections points A and B
\fill[black,shift={(3.9270,-.707)}] circle(1.5pt) node[below] {\tiny B};
\fill[black,shift={(.7854,.707)}] circle(1.5pt) node[above] {\tiny A};
% draw the exact coordinates of A and B
\draw[] (5,-.95) node[below] (3) {\tiny A$\left(\frac{\pi}{4},\frac{\sqrt2}{2}\right)$ \quad \tiny B$\left(\frac{5}{4}\pi,-\frac{\sqrt2}{2}\right)$};
\end{tikzpicture}
\end{document}
答案3
还有一种使用 tikz 库的方法datavisualization.formats.functions
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary {datavisualization.formats.functions}
\begin{document}
\begin{tikzpicture}[]
\datavisualization [school book axes,
visualize as smooth line/.list={sin,cos},
sin={style={red,very thick}, label in legend={text=$\sin(x)$}},
cos={style={blue,very thick},label in legend={text=$\cos(x)$}},
all axes={ticks={step=0.5}},
x axis={grid={step=0.25},
length=13cm,
ticks={tick unit=\pi},
label=$x$,
max value=2.25},
y axis={grid,length=6cm,label=$y$,max value=1.25}
]
data [set=sin,format=function] {
var x : interval [0:2];
func y = sin(\value x * pi r);
}
data [set=cos,format=function] {
var x : interval [0:2];
func y = cos(\value x * pi r);
};
\end{tikzpicture}
\end{document}