# plotting discrete signals

I would like to plot several discrete signals with pgfplots. So far, I've done the following

\documentclass[border={10pt}]{standalone}
\usepackage{tikz,pgfplots,filecontents,amsmath}
\pgfplotsset{compat=1.5}

\begin{filecontents}{data.dat}
n   xn
-5   0.0
-4   0.0
-3   3.0
-2   2.0
-1   1.0
0   0.0
1   1.0
2   2.0
3   3.0
4   0.0
5   0.0
\end{filecontents}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
axis x line=middle,
axis y line=middle,
every axis x label={at={(current axis.right of origin)},anchor=north west},
every axis y label={at={(current axis.above origin)},anchor= north west},
every axis plot post/.style={mark options={fill=black}},
xlabel={$n$},
ylabel={$\boldsymbol{x[n]}$},
xtick={-7, -6,-5,-4,-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8},
ymin=0,
ymax=5.5,
]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{axis}
\end{tikzpicture}

\end{document}


The first question is that changing xtick doesn't seem to affect the appearance of the axis, how to expand the xtick? The second question is some signals may have negative values, therefore, the lines will overlap the ticks. My question is is there a way to place the xtick above the axis?

-
Part 1 is easy, add xmin and xmax to set the limits explicitly, just like you did for the limits of the y axis. The second part is harder. Do you want to place the tick (and ticklabel I guess) above the axis only in case of a negative signalvalue? I don't know if/how that can be done. – hugovdberg Feb 27 at 10:00
Without explicitly adding xmin and xmax, you can also set enlarge x limit={abs=0.5} which is what I suspect you want. See tex.stackexchange.com/questions/294398/… – Rmano Feb 27 at 10:19

The xtick and xmin and xmax do not affect each other, as a result, just because you have xtick going down to -7 won't make xmin go down that far either.

Now, regarding placing the ticks above the axis, this can be easily done for all of them as shown in the example below. If, however, you want to have some ticks above and some ticks below, then I'm not sure how to do it. As an alternative, you could have the ticks slightly offset so they don't overlap with the lines.

### Negative Values, Ticks Above

\documentclass[border={10pt}, convert]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{filecontents}{data.dat}
n   xn
-5   -0.0
-4   -0.0
-3   -3.0
-2   -2.0
-1   -1.0
0   -0.0
1   -1.0
2   -2.0
3   -3.0
4   -0.0
5   -0.0
\end{filecontents}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
xlabel={$n$},
ylabel={$\boldsymbol{x[n]}$},
ylabel style={
at={(yticklabel cs: 0.1, 5pt)},
},
xtick={-6, -5, ..., 6},
xmin=-7, xmax=7,
ymin=-5, ymax=1,
xticklabel style={
anchor=south,
inner sep=5pt,
}
]
\addplot [ycomb, black, thick, mark=*] table [x={n}, y={xn}] {data.dat};
\end{axis}
\end{tikzpicture}

\end{document}


### Pos/Neg Values, Ticks Offset

\documentclass[border={10pt}, convert]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{filecontents}{data.dat}
n   xn
-5   -0.0
-4   -0.0
-3   -3.0
-2   -2.0
-1   -1.0
0   -0.0
1   1.0
2   2.0
3   3.0
4   0.0
5   0.0
\end{filecontents}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
xlabel={$n$},
ylabel={$\boldsymbol{x[n]}$},
% xtick={-6, -5, ..., 6},
xmin=-7, xmax=7,
ymin=-5, ymax=5,
xticklabel style={
anchor=north east,
inner sep=2pt,
}
]
\addplot [ycomb, black, thick, mark=*] table [x={n}, y={xn}] {data.dat};
\end{axis}
\end{tikzpicture}

\end{document}


### Tick Label Background

As suggested by Rman, another option is to add a background to the ticks so that they are visible over the graphics. He also mentions the use of enlarge x limits which is a way of making xmin and xmax bigger. In particular, enlarge x limits={abs=1.5} will automatically calculate xmin and xmax, and then add -1.5 and 1.5 units respectively to each value.

Since I don't want a completely white box, I have opted to use a translucent white background so that the line can still be seen underneath. Also note that axis on top is required; otherwise, the ticks end up underneath.

\documentclass[border={10pt}, convert]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{filecontents}{data.dat}
n   xn
-5   -0.0
-4   -0.0
-3   -3.0
-2   -2.0
-1   -1.0
0   -0.0
1   1.0
2   2.0
3   3.0
4   0.0
5   0.0
\end{filecontents}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
xlabel={$n$},
ylabel={$\boldsymbol{x[n]}$},
% xtick={-6, -5, ..., 6},
enlarge x limits={abs=1.5},
ymin=-5, ymax=5,
xticklabel style={
fill=white,
fill opacity=0.7,
text opacity=1,
inner sep=1pt,
},
axis on top,
]
\addplot [ycomb, black, thick, mark=*] table [x={n}, y={xn}] {data.dat};
\end{axis}
\end{tikzpicture}

\end{document}


-
+1, nice answer, but aesthetically I think using a fill for the ticks label (and let them where they are, above or below the axes depending on where most of values are) is much more appropriate... see tex.stackexchange.com/questions/211562/… – Rmano Feb 27 at 10:23
...although I am struggling making the labels in front of the stems. Hmmm.... – Rmano Feb 27 at 10:36
@Rmano Thanks for the suggestion. I implemented it :) – JP-Ellis Feb 27 at 10:44

I've reached to a satisfactory result. The credit goes to this answer.

\documentclass[border={10pt}]{standalone}
\usepackage{tikz,pgfplots,filecontents,amsmath}
\pgfplotsset{compat=1.5}
%\pgfplotsset{plot coordinates/math parser=false}

\begin{filecontents}{data.dat}
n   xn
-4   -1.0
-3   -1.0
-2   -1.0
-1   -1.0
0    0.0
1    1.0
2    1.0
3    1.0
4    1.0
\end{filecontents}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
scale=1.3,
axis x line=middle,
axis y line=middle,
every axis x label={at={(current axis.right of origin)},anchor=north west},
every axis y label={at={(current axis.above origin)},anchor= north west},
every axis plot post/.style={mark options={fill=black}},
xmin=-5.5,
xmax=5.5,
xtick={1, 2, 3, 4},
xticklabels={1, 2, 3, 4},
extra x ticks={-4, -3, -2, -1},
extra x tick labels={-4, -3, -2, -1},
extra x tick style={ xticklabel style={yshift=0.5ex, anchor=south} },
xlabel={$\boldsymbol{n}$},
ylabel={$\boldsymbol{y[n]}$},
ytick={-2,-1, 0, 1, 2},
ymin=-1.5,
ymax=1.5,
]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\addplot+[ycomb,black, very thick] table [x={n}, y={xn}] {data.dat};
\end{axis}
\end{tikzpicture}

\end{document}

-