我希望能够在图表上显示两个函数,一个是蓝色矩形,另一个是任何信号。此外,我希望能够说明信号的名称、宽度、高度,并且不显示线号。让我解释一下,我能够制作以下图表:
我想要的是放置黑色信号 (f) 的名称,并指出矩形的起点和终点 (t、t+W)。还有矩形的高度 (1/W)。我对在水平和垂直线上显示线号 (-2、-1.5、...、1.5、2) 不感兴趣。
这是代码
\documentclass[]{article}
\usepackage{tikz}
\usepackage{pgfplots}
%\pgfplotsset{width=8cm,compat=1.3} %gráficas
\pgfplotsset{compat=1.5,
every mark/.append style={scale=1},
scale only axis,
}
\usetikzlibrary{arrows,intersections,plotmarks}
\tikzset{shorten <>/.style = {shorten <=#1, shorten >=#1}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
height=5.5cm,
width=0.8\textwidth,
axis lines=middle,
grid, % <--- added
grid style = {dashed}, % <--- added
xlabel=$t$, xlabel style={anchor=west},
%ylabel=$f(t)$, ylabel style={anchor=south},
xmin=-2.3, xmax=2.3, % <--- changed
ymin=-3, ymax=3 % <--- changed
]
%Below the red parabola is defined
\addplot [
domain=-2.3:2.3,
samples=200,
color=black,
line width=1pt,
]
{sin(deg(2*x)) + cos(deg(5*x))}; % 4*(x- 1/2)^2
\draw[blue,thick] (axis cs:-0.5,0) rectangle (axis cs:0.8,1);
%\draw[black] (axis cs:0.2,0) rectangle (axis cs:0.8,0);
\end{axis}
\end{tikzpicture}
\end{document}
我将非常感谢您的帮助。
答案1
不要避开 PGFPlots 并尝试此代码:
\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
height=5.5cm, width=0.8*\textwidth,
axis lines=middle,
grid, grid style={dashed},
xlabel=$t$, xlabel style={anchor=west},
ylabel=$f(t)$, ylabel style={anchor=south},
xmin=-2.3, xmax=2.3,
ymin=-3, ymax=3,
xticklabels=none, yticklabels=none,
]
\addplot [
domain=-2.3:2.3,
samples=100,
thick, smooth,
] {sin(deg(2*x)) + cos(deg(5*x))};
\draw[blue, thick, font=\small] (-0.5,0) node[below, black]{$t$} -- (-0.5,1) --node[pos=0.8, above, black]{$1/W$} (0.8,1) -- (0.8,0) node[below, black]{$t+W$} -- cycle;
\end{axis}
\end{tikzpicture}
\end{document}
编辑:我错过了“f”-使用
...
] {sin(deg(2*x)) + cos(deg(5*x))} node[pos=0.75, above right]{$f$};
答案2
避免使用 pgfplots 并尝试以下代码:
\documentclass[]{article}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
\draw[lightgray,dotted,xstep=1,ystep=1] (-2,-3) grid (2,3);
\draw[black,->] (-2.3,0)--(2.3,0) node[right] {$x$};
\draw[black,->] (0,-3)--(0,3) node[left] {$y$};
\draw[smooth] plot[domain=-2.3:2.3] (\x,{sin(2*\x r)+cos(5*\x r)});
\draw[blue,thick] (-0.5,0) rectangle (0.8,1);
\node[below] at (-.5,0) {\small $t$};
\node[below] at (.8,0) {\small $t+W$};
\node[above] at (.7,1) {\small $1/W$};
\end{tikzpicture}
\end{document}
输出: