\documentclass[12pt]{beamer}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}
\begin{figure}%
\begin{tikzpicture}[scale=0.8]
\begin{axis}[axis lines=middle, xtick={-3,-2,...,5,9},
xticklabels={-3,{}, -1, 0, 1, {}, {}, {}, 5, $k$},
extra x ticks={-2, 2, 3, 4},
extra x tick labels={$-2$, $2$, $3$, $4$},
extra x tick style={
xticklabel style={yshift=0.5ex, anchor=south}},
xmin=-5,xmax=11, ytick={\empty}, yticklabels={},
ymin=-5, ymax=6, axis on top]
\addplot+[ycomb,black,thick] plot coordinates
{(-3,3) (-2,-2) (-1,1.5) (0,4) (1,1.5) (2,-1.5) (3, -3)
(4,-1) (5,4) (9,5)};
\end{axis}
\node at (5.1,3.5) {$\cdots$};
\node at (0,3.5) {$\cdots$};
\node at (0.8,4.5) {\tiny{$x[-3]\delta[n+3]$}};
\node at (2.7,4.8) {\tiny{$x[0]\delta[n]$}};
\node at (4.2,5) {\tiny{$x[5]\delta[n-5]$}};
\node at (6,5.5) {\tiny{$x[\alert{k}]\delta[n-\alert{k}]$}};
\node at (6.8,2.2) {$n$};
\node at (2.7,5.7) {$x[n]$};
\end{tikzpicture}
\end{figure}
\end{document}
在本论坛中搜索了一番之后,我找到了此代码和相关情节。我想要的东西的定位是一个临时解决方案。
问题:所有定位都是手动的(通过反复试验)。即使是 x 轴和 y 轴标签,我也必须通过反复试验来定位。这可以自动化吗?当我更改 x 标签的字体大小(例如 \footnotesize)时,它们的对齐方式会变得混乱。
答案1
您可能更喜欢更灵活的自己绘制方法,而不是尝试自定义 PGF 图。以下是使用MetaPost和luamplib
. 用 编译lualatex
。
\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\usepackage{unicode-math}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
numeric u, v;
u = 1cm;
v = 8mm;
color pin_shade;
pin_shade = 1/3[blue,white];
vardef make_pin(expr X_value, Y_value, label_text) =
draw (X_value*u,0) -- (X_value*u,Y_value*v) withpen pencircle scaled 0.8 withcolor pin_shade;
fill fullcircle scaled 4 shifted (X_value*u,Y_value*v) withcolor pin_shade;
numeric label_Y, number_Y;
label_Y = Y_value*v * (abs(Y_value*v)+10)/abs(Y_value*v);
number_Y = if Y_value > 0: - fi 9;
label(label_text, (X_value*u, label_Y));
label("$" & decimal X_value & "$", (X_value*u if X_value=0: +5 fi, number_Y));
enddef;
beginfig(1);
path xx, yy;
xx = (4 left -- 8 right) scaled u;
yy = (4 down -- 5 up) scaled v;
drawarrow xx withcolor .5 white;
drawarrow yy withcolor .5 white;
make_pin(-3, 2,"$\scriptstyle x[-3]δ[n+3]$");
make_pin(-2,-1,"$\scriptstyle x[-2]δ[n+2]$");
make_pin(-1,+1,"$\scriptstyle x[-1]δ[n+1]$");
make_pin( 0,+3,"$\scriptstyle x[0]δ[n]$");
make_pin(+1,+1,"$\scriptstyle x[1]δ[n+2]$");
make_pin(+2,-1,"$\scriptstyle x[2]δ[n+3]$");
% etc
endfig;
\end{mplibcode}
\end{document}
答案2
您可以使用软件包提供的常规轴标签pgfplots
。要向您的图添加注释,请tikz
结合使用节点,axis cs
这样您就可以使用您已用于图的坐标。
\documentclass{beamer}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle, xtick={-3,-2,...,5,9},
xticklabels={-3,{}, -1, 0, 1, {}, {}, {}, 5, $k$},
extra x ticks={-2, 2, 3, 4},
extra x tick labels={$-2$, $2$, $3$, $4$},
extra x tick style={
xticklabel style={yshift=0.5ex, anchor=south}},
xmin=-5,xmax=11, ytick={\empty}, yticklabels={},
ymin=-5, ymax=6, axis on top,
% axis labels
xlabel={$n$}, x label style={anchor=north},
ylabel={$x[n]$}, y label style={anchor=west}]
\addplot+[ycomb,black,thick] plot coordinates
{(-3,3) (-2,-2) (-1,1.5) (0,4) (1,1.5) (2,-1.5) (3, -3)
(4,-1) (5,4) (9,5)};
% point labels
\node[anchor=south] at (axis cs:-3,3) {\tiny{$x[-3]\delta[n+3]$}};
\node[anchor=south west] at (axis cs:0,4) {\tiny{$x[0]\delta[n]$}};
\node[anchor=south] at (axis cs:5,4) {\tiny{$x[5]\delta[n-5]$}};
\node[anchor=south] at (axis cs:9,5) {\tiny{$x[\alert{k}]\delta[n-\alert{k}]$}};
% alternative positioning for cdots
\node at (axis cs:-4.5,1.75) {$\cdots$};
\node at (axis cs:7.5,1.75) {$\cdots$};
\end{axis}
\end{tikzpicture}
\end{document}