[Tex/LaTex] pgfplots: enlarge x limits while using x min/max

pgfplots

The problem: I like to use enlarge x limits to have some white space to the axes. I now often load in my data (generated elsewhere) but only want to plot a specific range (using x min/max). Now, doing so will not end up in white space since there is more data outside the plotted range. Is there an easy (built-in) way to 'cut-off' the plotted data at the position defined by x min/max and enlarge x limits?

MWE:

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=0.85\linewidth,
height=5cm,
enlarge x limits=0.05,
enlarge y limits=0.05,
xmin = 2,
xmax = 6
]

\addplot[black] coordinates {
(1, 2)
(2, 2.5)
(3, 1.5)
(4, 1.4)
(5, 1.3)
(6, 1.1)
(7, 0.9)
};
\end{axis}
\end{tikzpicture}
\end{document}

Best Answer

To actually filter out the values that fall out of the desired range, use restrict x to domain=<xmin>:<xmax> instead of xmin=<xmin> and xmax=<xmax>:

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=0.85\linewidth,
height=5cm,
enlarge x limits=0.05,
enlarge y limits=0.05,
restrict x to domain=2:6
]

\addplot[black] coordinates {
(1, 2)
(2, 2.5)
(3, 1.5)
(4, 1.4)
(5, 1.3)
(6, 1.1)
(7, 0.9)
};
\end{axis}
\end{tikzpicture}
\end{document}