[Tex/LaTex] Small x axis range in pgfplots

pgfplots

I try to plot data in pgfplots. My data file has 1001 points in a range (x axis) from 2499999500 to 2500000500, so I have steps of 1. I get the message “Package pgfplots Warning: Axis range for axis x is approximately empty; enlarging it (it is [2499999500.0000000:2500000500.0000000]) …” in the log file and a straight line is drawn, the axis limits range from 2e9 to 3e9, overriding them with xmin = ... etc. does not help. Any idea what can be done?

File test.tex:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
      \begin{axis}[
        xmin=2499999500,
        xmax=2500000500,
      ]
      \addplot+ table [x index=0, y index=1] {testdata.dat};
    \end{axis}
\end{tikzpicture}

\end{document}

File testdata.dat:

2499999500 0
2500000000 1
2500000500 0

Best Answer

Not quite an answer but a workaround is using an expression such as:

\addplot+ table [x expr=\thisrow{x}-2500000000] {\mytable};

to subtract a large common number from the dataset. This still uses the math engine of pgf and works for your small test case.

\documentclass[border=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepackage{filecontents}
\begin{filecontents}{testdata.dat}
2499999500 0
2500000000 1
2500000500 0
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xtick=data, xticklabels from table={testdata.dat}{[index]0}]
  \addplot+ table [x expr=\thisrowno{0}-2500000000] {testdata.dat};
\end{axis}
\end{tikzpicture}
\end{document} 

enter image description here

For closely spaced data this still breaks down. The precision of the FPU engine can be shown with

\pgfkeys{/pgf/fpu=true}
\pgfmathparse{2500000400 - 2500000500}
\pgfmathprintnumber{\pgfmathresult}
\pgfkeys{/pgf/fpu=false} 

Which prints zero as the change occurs at the 8th digit. So I would recommend using either gnuplot as suggested in the pgfmanual or preprocessing the dataset externally.

Alternatively the fp package is more suited to the problem as it operates with fixed point. I do not know how to integrate it with pgfplots.


Update:

Whereas with LaTeX the precision is limited it is not a problem to use LuaTeX for the calculation here and the results are quite nice. If run with LuaTeX the following example produces the correct output:

\documentclass[tikz,preview]{standalone}
\usepackage{tikz,pgfplots,pgfplotstable}
\pgfplotsset{compat=1.8} 

\begin{document}

\pgfplotstableread
{
x y 
2499999500  1
2499999501  2
2499999502  3
2499999503  4
2499999504  5
2499999505  6
2499999506  7
2499999507  8
2499999508  9
2499999509  10
}\mytable;

\begin{tikzpicture}
    \begin{axis}
    \addplot+ table [x expr=\directlua{tex.print(\thisrow{x}-2499999500)},y=y] {\mytable}; 
    \end{axis}
\end{tikzpicture}

\end{document}

New example with lua