# Plotting a Function in pgfplots with a Gap in the Domain

I recently received help plotting the function here. I would now like to remove a portion of the graph. I want to remove the part with -2<= x <= 1. When I comment it out, pgfplots fills in the gap with a straight line. But, I would like that portion of the graph to disappear entirely and result in a function with a disconnected domain. Is that possible?

• You can replace the expression for (-2, 1] with: and(\x>-2, \x<=1) * inf and add unbounded coords=jump. Unfortunately, you need to tell pgfplots to use the x values -2 and 1 by setting the samples value higher (say samples=400) or by providing explicit x values with samples at={-5, -4.5, ..., -2, 1, 1.01, 1.5, 2, 2.1, 2.2, ..., 5}. – Qrrbrbirlbel Sep 9 '13 at 23:04

Simply use two \addplot commands with different domains such that the region you don't want is excluded.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}[
declare function={
func(\x)= (\x<=-2) * (\x*\x + 6*\x + 8)   +
and(\x>-2, \x<=1) * (2 - \x - \x*\x)     +
and(\x>1,  \x<=2) * (6 - 8*\x + 2*\x*\x) +
(\x>2) * (-10 + 6*\x - \x*\x);
}
]
\begin{axis}[
axis x line=middle, axis y line=middle,
ymin=-5, ymax=5, ytick={-5,...,5}, ylabel=$y$,
xmin=-5, xmax=5, xtick={-5,...,5}, xlabel=$x$,
]
\pgfplotsinvokeforeach{-2, 1, 2}{
\draw[dashed] ({rel axis cs: 0,0} -| {axis cs: #1, 0}) -- ({rel axis cs: 0,1} -| {axis cs: #1, 0});}