Say I want to plot the function |sin(x)|
and its derivative, which has a discontinuity at x = 0
, using pgfplots
. How can I handle the discontinuity? What I'd like to happen is for the red line below to end at (0,-1)
, then jump to (0,1)
and continue normally from there. My main issue here is having two points with the x-coordinate 0
, I think.
I do not want to have two separate plots (as in this question, which is actually about division), as this would mess with automatic selection of the line style and other features of pgfplots
(e.g. a single entry in the legend).
If I read the documentation correctly, I could achieve the desired effect by explicitly providing the coordinates (0,-1), (0,nan), (0,1)
and setting unbounded coords=jump
, but I want to plot the function without specifying all the coordinates. Specifying the coordinate of the discontinuity is fine, of course.
A starting point:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
trig format plots=rad,
domain=-pi:pi,
samples=201,
no markers,
xtick={-pi, 0, pi},
xticklabels={\(-\pi\), \(0\), \(\pi\)},
ytick={-1, 0, 1},
grid=major,
typeset ticklabels with strut,
]
\addplot {abs(sin(x))};
\addplot {sign(x) * cos(x)};
\end{axis}
\end{tikzpicture}
\end{document}
pgfplots
.\usepackage{pgfplots}
andaxis
environments, how is it not usingpgfplots
?\addplot
commands to draw the different segments of the plot, though, which is not what I want, as outlined above.