[Tex/LaTex] pgfplots: dimension too large when reading data from file

pgfplots

Based on the following data table data.dat

    x    a    b    c    d
    0    284.15    69.18    284.15    70.26
    0.03    284.15    70.20    284.15    70.04
    0.04    318.15    70.20    284.15    70.04
    0.1    318.15    70.20    284.15    70.04
    1    318.15    70.20    284.15    70.04
    5    314.75    70.20    284.13    70.04
    6    314.75    70.20    284.12    70.04
    10    314.75    70.20    284.10    70.05
    15    314.75    70.20    284.08    70.05
    20    284.05    70.20    284.05    70.05
    30    284.00    70.19    284.00    70.06
    50    283.90    70.19    283.90    70.07
    100    283.66    70.18    283.66    70.09
    200    283.60    70.17    283.58    70.12
    300    284.55    70.15    284.50    70.12
    400    286.12    70.11    286.06    70.09
    500    287.87    70.04    287.81    70.03
    600    289.58    69.95    289.53    69.94
    1000    294.97    69.31    294.94    69.32
    1500    298.86    67.98    298.85    68.00
    2000    300.84    66.05    300.84    66.09
    2500    301.64    63.48    301.84    63.53
    3000    301.60    60.19    301.62    60.27
    3500    300.85    56.08    300.88    56.19
    4000    299.45    50.95    299.49    51.10
    4500    297.31    44.46    297.38    44.69
    5000    294.20    35.93    294.33    36.31
    5500    289.36    23.33    289.64    24.14
    5700    286.21    15.29    286.73    16.73
    5710    286.00    14.77    286.55    16.28

I'm trying to plot the third and fith column for x in [0,20]. This is my MWE, that results in an error "dimension too large".

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    width=0.45\textwidth,
    xmin=-2,
    xmax=20,
    ymin=70,
    ymax=72
]
    \addplot
    table[x index=0, y expr={\thisrowno{2}+1.01325}, header=true] {data.dat};

    \addplot
    table[x index=0, y expr={\thisrowno{4}+1.01325}, header=true] {data.dat};
  \end{axis}
\end{tikzpicture}
\end{document}

If I crop the data table or use the environment filecontents, no error occurs.
Some choices of width and xmax work, while others don't. In general, a larger width seems to require a larger xmax.

Does anyone know the reason for the error?

Best Answer

This happens because PGFPlots internally plots all the values, even the ones that aren't in the visible range, and only clips the plots to the visible range at the very end. This leads to part of the points lying very far outside the axis, too far for TeX to handle.

You can filter the data using restrict x to domain=-2:20 and restrict y to domain=70:72 to remove the points you aren't interested in from the data stream. The limits can still be set using xmin, xmax, etc.:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents*}{data.dat}
    x    a    b    c    d
    0    284.15    69.18    284.15    70.26
    0.03    284.15    70.20    284.15    70.04
    0.04    318.15    70.20    284.15    70.04
    0.1    318.15    70.20    284.15    70.04
    1    318.15    70.20    284.15    70.04
    5    314.75    70.20    284.13    70.04
    6    314.75    70.20    284.12    70.04
    10    314.75    70.20    284.10    70.05
    15    314.75    70.20    284.08    70.05
    20    284.05    70.20    284.05    70.05
    30    284.00    70.19    284.00    70.06
    50    283.90    70.19    283.90    70.07
    100    283.66    70.18    283.66    70.09
    200    283.60    70.17    283.58    70.12
    300    284.55    70.15    284.50    70.12
    400    286.12    70.11    286.06    70.09
    500    287.87    70.04    287.81    70.03
    600    289.58    69.95    289.53    69.94
    1000    294.97    69.31    294.94    69.32
    1500    298.86    67.98    298.85    68.00
    2000    300.84    66.05    300.84    66.09
    2500    301.64    63.48    301.84    63.53
    3000    301.60    60.19    301.62    60.27
    3500    300.85    56.08    300.88    56.19
    4000    299.45    50.95    299.49    51.10
    4500    297.31    44.46    297.38    44.69
    5000    294.20    35.93    294.33    36.31
    5500    289.36    23.33    289.64    24.14
    5700    286.21    15.29    286.73    16.73
    5710    286.00    14.77    286.55    16.28
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    width=0.45\textwidth,
    xmin=-2,
    xmax=20,
    restrict x to domain=-2:20,
    ymin=70,
    ymax=72,
    restrict y to domain=70:72
]
    \addplot
    table[x index=0, y expr={\thisrowno{2}+1.01325}, header=true] {data.dat};

    \addplot
    table[x index=0, y expr={\thisrowno{4}+1.01325}, header=true] {data.dat};
  \end{axis}
\end{tikzpicture}
\end{document}