1

enter image description here

\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}

After some more searching in this forum, I have come up with this code and the associated plot. The positioning of the things I want is a kludge.

Questions: All the positioning is manual (by trial and error). Even the x- and y-axis labels I had to locate by trial and error. Can this be automated? When I change the fontsize for the x-labels (e.g., \footnotesize), their alignmens get messed up.

epR8GaYuh
  • 2,432
CSR
  • 175
  • 6
    May be you could take a look at this http://tex.stackexchange.com/questions/296223/plotting-discrete-signals?rq=1 – Raaja_is_at_topanswers.xyz Dec 09 '16 at 14:56
  • If you can draw that neatly, you might just scan your drawing and include it as a graphic. – Thruston Dec 09 '16 at 18:55
  • Thanks for the responses. I had already looked at the links that are being referred to. My question is about adding labels to the individual stems. If you can provide a link to a post that details how to do that that would be helpful. – CSR Dec 10 '16 at 00:58

2 Answers2

2

You might prefer a more flexible draw-it-yourself approach rather than trying to customise a PGF plot. Here's an approach using MetaPost and luamplib. Compile with lualatex.

enter image description here

\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}
Thruston
  • 42,268
1

You can use the normal axis labels provided by the pgfplots package. To add annotations to your plot, use tikz nodes combined with axis cs which lets you use the the coordinates you already used for your plot.

\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}
epR8GaYuh
  • 2,432