I'm quite new to pgfplots
and I must be doing something wrong. I'm plotting a piecewise function using the following code:
\documentclass{amsbook}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\newcommand{\DELTA}{.3}
\pgfmathdeclarefunction{fdelta}{1}{%
\pgfmathparse{%
and(#1 >= 1-\DELTA/2, #1 <= 1+\DELTA/2) * (1 - 2 * abs(#1 - 1) / \DELTA)
}
}
\begin{tikzpicture}
\begin{axis}[
title={$f_{\delta}(x)$},
xmin=0, xmax=2,
xtick={0, .85, 1.15, 2},
xticklabels={0, \(1-\frac{1}{2}\delta\), \(1+\frac{1}{2}\delta\), 2},
ytick={0, 1},
]
\addplot[domain=0:2, ultra thick]{fdelta(x)};
\end{axis}
\end{tikzpicture}
\end{document}
Now if I add the samples=11
option to \addplot
, the picture changes position in the resulting PDF file! If I keep increasing the sample size, the picture eventually moves outside of the margins of the document...
In addition to the moving plot issue, finding an appropriate sample size for a piecewise linear function is a bit frustrating because the samples often "miss" the junction points. Is there a way to set samples piecewise as well?
As a secondary question, is it possible to set the values of xtick
using \DELTA
, as in the function definition?
Thanks!
Note: my example uses amsbook
, but I observe the same behavior with standalone
.
%
signs at the end of every line in your function definition.\pgfmathsetmacro{\tickone}{1-\DELTA/2}\pgfmathsetmacro{\ticktwo}{1+\DELTA/2}
thenxtick={0,\tickone,\ticktwo,2}
.