我想在 Tikz/pgfplots 中重新创建以下茎图。到目前为止,我能够做到以下几点。我希望能够控制图形窗口的元素。对于这种特殊情况:删除 y 轴标签和刻度,将 y 标签旋转 90 度 {x(n)} 并放置在其他位置,保留 x 轴标签和刻度并将它们与图对齐,然后在图中添加 y 值。
\documentclass{beamer}
\usetheme{Boadilla}
\usepackage{tikz, pgfplots}
\usetikzlibrary{positioning, calc}
\begin{document}
\begin{frame}{Using PGF Plots}
\begin{tikzpicture}
\begin{axis}[
every outer y axis line/.style={draw=none},
tick style={draw=none},
xlabel=n,ylabel=x(n),
% this will put the y-values
nodes near coords,
point meta = y
]
\addplot[ycomb,color=blue,mark=o, very thick] coordinates {
(-3,0) (-2,4) (-1,0) (0,2) (1,0) (2,0) (3,-1)
};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
答案1
这使得它们非常相似。为了去掉 y 轴,我们使用
axis y line=left,
y axis line style={opacity=0},
ytick=\empty,
为了消除不需要的刻度和节点,我们采用了比较。此外,还有一些其他更改(参见代码)。(我原本以为这里不需要回收,\pgfmathparseFPU
但也许我遗漏了一些东西。)
\documentclass{beamer}
\usetheme{Boadilla}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\newcommand{\pgfmathparseFPU}[1]{\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathparse{#1}%
\pgfmathsmuggle\pgfmathresult\endgroup}
\begin{document}
\begin{frame}{Using PGF Plots}
\begin{tikzpicture}
\begin{axis}[axis x line=middle,
x axis line style={thick},
enlarge x limits,
axis y line=left,
y axis line style={opacity=0},ytick=\empty,
xticklabel=\ifdim\tick pt<3pt $\pgfmathprintnumber{\tick}$\fi,
tick style={draw=none},
xlabel=$n$,ylabel=$x(n)$,
nodes near coords={\pgfmathparseFPU{\pgfplotspointmeta}%
\ifdim\pgfmathresult pt=0pt\else \pgfmathprintnumber\pgfplotspointmeta\fi},
nodes near coords align=left,
point meta = y
]
\addplot[ycomb,color=blue,mark=*, very thick] coordinates {
(-3,0) (-2,4) (-1,0) (0,2) (1,0) (2,0) (3,-1)
};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}