我尝试不使用坐标而是通过定义全局函数来绘制正方形。但是,我得到的是倾斜的线条。是否可以得到直线,这样我就可以得到一个漂亮的矩形?
\documentclass{beamer}
\usepackage{pgfplots}
\begin{document}
\begin{frame}{Dirac puls}
\begin{center}
\begin{tikzpicture}[
declare function={
func(\x)= (\x<=-0.5) * (0) +
and(\x>-0.5, \x<=0.5) * (1) +
(\x>0.5) * (0);
}
]
\begin{axis}[
axis lines = center,
xlabel = \(t\),
ylabel = {\(\delta{(t)}\)},
ymin=0,
ymax=2,
every axis plot/.append style={ultra thick},
tick align = inside,
clip mode = individual
]
\addplot[red, domain=-1:1]{func(x)};
\end{axis}
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}
提前致谢
答案1
更改代码行
\addplot[red, domain=-1:1]{func(x)};
到
\addplot[red, domain=-1:1, samples=200]{func(x)};
解决了问题。使用更多样本会使线条更加垂直,但也需要更长的计算时间。
更新代码:
\documentclass{beamer}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{frame}{Dirac puls}
\begin{center}
\begin{tikzpicture}[
declare function={
func(\x)= (\x<=-0.5) * (0) +
and(\x>-0.5, \x<=0.5) * (1) +
(\x>0.5) * (0);
}
]
\begin{axis}[
axis lines = center,
xlabel = \(t\),
ylabel = {\(\delta{(t)}\)},
ymin=0,
ymax=2,
every axis plot/.append style={ultra thick},
tick align = inside,
clip mode = individual
]
\addplot[red, domain=-1:1, samples=200]{func(x)};% <-- added samples=200
\end{axis}
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}
输出: