11

I am trying to plot data from data files which are using the comma as decimal separator instead of a point (as is normal in SI style (French version): http://en.wikipedia.org/wiki/Decimal_mark#Examples_of_use ). However, even after excessive search I cannot find an option that would actually tell pgfplots to read the table with the comma as decimal separator. Minimal (not) working sample:

\documentclass[paper=a4,12pt,version=last,landscape]{scrartcl}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\centering
\begin{tikzpicture}
    \begin{axis}[
            width=0.9\textwidth,
            height=0.5\textheight,
            xlabel={Standardweg~[mm]},
            ylabel={Standardkraft~[N]},
            grid=major,
            ]
        \addplot table[x=Standardweg, y=Standardkraft] {Messwerte.TRA};
    \end{axis}
\end{tikzpicture}
\end{document}

Messwerte.TRA:

Standardweg  Standardkraft
0,000000000000e+000  1,960904836655e+000
-2,349615044750e-004     3,081407308578e+000
-2,349615044750e-004     4,164415359497e+000
-2,349615044750e-004     5,441759109497e+000
-2,349615044750e-004     6,443712234497e+000
-2,349615044750e-004     7,598009109497e+000
-2,349615044750e-004     8,951524734497e+000
-6,029571522959e-001     1,002574348450e+001
-2,349615044750e-004     1,122496223450e+001
-2,349615044750e-004     1,252183723450e+001

On compilation, I get the error: ! Package PGF Math Error: Could not parse input '-2,349615044750e-004' as a floating point number, sorry. The unreadable part was near ',349615044750e-004'.. for every single number in the .TRA-file.

I know that I could find and replace every , with a . in the files, but that is not an option considering the amount of files I have to evaluate.

Also, I tried stuff like /pgf/number format/use comma, set decimal separator={{,}}, \pgfkeys{/pgf/number format/.cd,fixed,precision=2,use period} and use comma, but none of that helped and I am pretty much running out of options. The documentation for pgfplots did not give any other ideas, either.

So: How do I convince pgfplots to actually read the comma as decimal separator and to not search for the point?

12
  • 1
    What about opening the file in a text editor and replacing commas with dots by the search&replace function? Feb 10 '14 at 15:11
  • 1
    Or, slightly more robust, use sed -i 's/\([[:digit:]]\),\([[:digit:]]\)/\1\.\2/g' *.dat to only replace commas that are surrounded by digits.
    – Jake
    Feb 10 '14 at 15:51
  • 1
    @Jake I mailed you my IBAN number :P
    – percusse
    Feb 10 '14 at 16:10
  • 3
    I have just implemented this in PGF CVS, i.e. some future version will offer an option read comma as period (it will advertise it in error messages as well). It is off by default because I anticipated problems with the math parser (which might have other meansings for comma). May 17 '14 at 20:54
  • 1
    @DenisBitouzé the feature is available in PGF, but since there has been no PGF release since 3.0.0, it is unavailable on CTAN. I don't know for sure if there is an unstable PGF build which contains it. It is used with \pgfmathprintnumber[read comma as period]{1234,56} Oct 3 '14 at 9:50
12

With an uptodate pgf/TikZ version (current on CTAN is 3.0.1a) it is now possible to use the new key /pgf/number format/read comma as period:

enter image description here

Code:

\documentclass[paper=a4,12pt,version=last,landscape]{scrartcl}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    width=0.9\textwidth,
    height=0.5\textheight,
    xlabel={Standardweg~[mm]},
    ylabel={Standardkraft~[N]},
    grid=major,
  ]
  \addplot table[
    x=Standardweg, y=Standardkraft,
    /pgf/number format/read comma as period% <---
    ] {
    Standardweg  Standardkraft
    0,000000000000e+000  1,960904836655e+000
    -2,349615044750e-004     3,081407308578e+000
    -2,349615044750e-004     4,164415359497e+000
    -2,349615044750e-004     5,441759109497e+000
    -2,349615044750e-004     6,443712234497e+000
    -2,349615044750e-004     7,598009109497e+000
    -2,349615044750e-004     8,951524734497e+000
    -6,029571522959e-001     1,002574348450e+001
    -2,349615044750e-004     1,122496223450e+001
    -2,349615044750e-004     1,252183723450e+001
  };
  \end{axis}
\end{tikzpicture}
\end{document}
12

As mentioned in the comments, the easiest thing to do is probably to replace the commas with periods in the data files (for example using sed -i 's/\([[:digit:]]\),\([[:digit:]]\)/\1\.\2/g' *.dat on Linux).

However, as Percusse points out, an alternative is to use the comma as an additional column separator by setting white space chars={{,},\ }, thereby splitting each number into two different columns, and merging them using

x expr=\thisrowno{0}.\thisrowno{1},
y expr=\thisrowno{2}.\thisrowno{3}

\documentclass[paper=a4,12pt,version=last,landscape]{scrartcl}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}


\begin{tikzpicture}
    \begin{axis}[
            width=0.9\textwidth,
            height=0.5\textheight,
            xlabel={Standardweg~[mm]},
            ylabel={Standardkraft~[N]},
            grid=major,
            ]
        \addplot table[x=Standardweg, y=Standardkraft, white space chars={{,},\ },
            x expr=\thisrowno{0}.\thisrowno{1},     y expr=\thisrowno{2}.\thisrowno{3}] {
        Standardweg  Standardkraft
0,000000000000e+000  1,960904836655e+000
-2,349615044750e-004     3,081407308578e+000
-2,349615044750e-004     4,164415359497e+000
-2,349615044750e-004     5,441759109497e+000
-2,349615044750e-004     6,443712234497e+000
-2,349615044750e-004     7,598009109497e+000
-2,349615044750e-004     8,951524734497e+000
-6,029571522959e-001     1,002574348450e+001
-2,349615044750e-004     1,122496223450e+001
-2,349615044750e-004     1,252183723450e+001
};
    \end{axis}
\end{tikzpicture}
\end{document}
1
  • Okay. This indeed solves the problem. Not the cleanest solution but still better than altering all the files (and also altering all the files that might still come!). The actual files have a few more columns, but that should easily be coverable. I guess I will go by your solution. Thanks a lot!
    – locutus
    Feb 10 '14 at 16: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.