# Calculate axis limits depending on depicted data

In a pgfplots axis environment, I want to make the axis limits dependent on the depicted data or some other data.

Consider the minimum working example

\documentclass{standalone}
\usepackage{pgfplots}

\begin{filecontents}{table_1.txt}
x y
1 1
2 2
\end{filecontents}

\begin{filecontents}{table_2.txt}
x y
1 2
2 1
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}
\end{document}


I want to set the xmin and xmax axis options such that the offset on both sides is xOffSet, so

xmin = minimum of all x data - xOffSet * (maximum of all x data - minimum of all x data)
xmax = maximum of all x data + xOffSet * (maximum of all x data - minimum of all x data)


How can I do this?

And is it also possible to use other data from the table files to set the axis limits?

• enlarge x limits options do this for you no? – percusse Mar 6 '15 at 13:05
• @percusse Indeed, that takes care of the offset depending on the depicted data. But now the second question I posed ;). How do I do this if the I want the axis limits to depend on other table data, so data from a table file that was not (and should not be) depicted? – Adriaan Mar 6 '15 at 13:12

This is what enlargelimits or enlarge x limits is for. If you set enlarge x limits=0.3, the axis limits will be increased by 30 % of the data range:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\begin{filecontents}{table_1.txt}
x y
1 1
2 2
\end{filecontents}

\begin{filecontents}{table_2.txt}
x y
1 2
2 1
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
enlarge x limits=0.3,
enlarge y limits=0.3
]

• @Adriaan: \addplot [draw=none] table {table_3.txt}; – Jake Mar 6 '15 at 13:14
• Indeed that's a way to do it :D. Quite a dirty one, but it works ;). Still, it would also be nice if you could just use expressions in axis limits like xmin and xmax. Like you can do so nicely with table data using x expr=.... – Adriaan Mar 6 '15 at 13:17
• @Adriaan: Sorry, I missed your comment before. Could you give an example of what kind of expression you would want to use in xmin and xmax? – Jake Mar 16 '15 at 11:07