12

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}
10

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}
  • 3
    Damn that was fast... – DaPhil Nov 11 '13 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.