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}
xmin
andxmax
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.xmin
andxmax
, you can also setenlarge x limit={abs=0.5}
which is what I suspect you want. See tex.stackexchange.com/questions/294398/…