0

Originally, I wanted to subtract the y value of the line from the y value of the last point, and display this difference on the plot. After going through many questions on this site, I tried to implement this. That got me part of the way, but while testing \pgfplotsconvertunittocoordinate I discovered the math is off by ~2. I'm open to any suggestions about changing my strategy, but why is my math slightly off?

This code...

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.csv}
s,t
5,430000
6,425209.22
\end{filecontents*}

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usetikzlibrary{calc}
\begin{document}

\begin{center}
    \begin{tikzpicture}
        \begin{axis}[title={Test},xmajorgrids=true,ymajorgrids=true,
                     scaled y ticks=false,y tick label style={/pgf/number format/fixed},ymin=355000,xtick=data,
                    ]
            \addplot[mark=*,thick,black] table[col sep=comma, x=s, y=t] {\jobname.csv}
                coordinate [pos=0] (start)
                coordinate [pos=1] (end);
            \coordinate (no) at (axis cs:5,356070); 
            \draw[red,dashed] (no) -- (no -| end);
            \draw[<->,dashed,shorten <=3pt] 
                let \p1 = (end), \p2 = (no) in 
                    (end) -- node[anchor=east]{\pgfplotsconvertunittocoordinate{y}{\y1}\pgfplotscoordmath{y}{datascaletrafo inverse to fixed}{\pgfmathresult}\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}} (no -| end);
        \end{axis}
    \end{tikzpicture}
\end{center}

\end{document}

...produced this result... resulting plot

...and I was expecting the node to read 425,209.22. I used Overleaf, which reports Package: pgfplots 2017/06/05 v1.15 Data Visualization (1.15) in the log.

aeroNotAuto
  • 2,390
  • 1
    The relative error is only few times 10^-6, which is impressive IMHO. This is LaTeX, not a computer algebra system. –  May 09 '19 at 20:28
  • @marmot I mean is any actual math being done? My testing attempt was intended to just regurgitate the value being fed in. – aeroNotAuto May 09 '19 at 21:47
  • AFAIK there is math done. LaTeX on its own does not deal with these large number, see e.g. here. So what's going on is that the numbers get handled with fpu (see section 56 Floating Point Unit Library of the pgfmanual, written by the author of pgfplots). This amounts to taking the logarithm and exponentiating things back. So one should not be surprised about tiniest inaccuracies. Of course, there are ways of deferring the computations to external programs, but if you want to stick with plain LaTeX then this is how it is. –  May 09 '19 at 22:03
  • This is why I prefer to do the math with C++ and use pgfplots just to display the data. – John Kormylo May 10 '19 at 03:49

0 Answers0