5

I am trying to do the following:

  1. Read data from a file [CSV]
  2. Add a calculated row to it [C]
  3. Transpose the data
  4. Plot the data
  5. Display the data in a table

Everything works so far, but the calculated row, and only the calculated row, is written in a scientific number format, althoug there is no reason for it (at least I don't see a clear reason why pgfplotstable should do it ...). I am trying to get rid of the scientific numbers to either get floating numbers or simple integers (as there will be no decimal place).

I have the following minimal working example:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Preamble
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use A4 paper
\documentclass[a4paper]{article}

% Use english language
\usepackage[english]{babel}
% Use UTF8 encoding
\usepackage[utf8]{inputenc}

% TikZ and PGF libraries for graphs
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}

% Booktabs for layout of tables
\usepackage{booktabs}

% Enable library for date-plotting
\usetikzlibrary{pgfplots.dateplot}

% Enable library for statistics [boxplot]
\usepgfplotslibrary{statistics}

% Always use newest version
\pgfplotsset{compat=newest,compat/show suggested version=false}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Pre - Document
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create data to test
\begin{filecontents}{testData.csv}
    day,        A,      B
    01,         10,     5
    02,         20,     10
    03,         100,    5
\end{filecontents}

% Create 'C' by calculating 'A - B'
\pgfplotstableset{
    create on use/C/.style={
        create col/expr={\thisrow{A}-\thisrow{B}},
    }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Document
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
    %%% Setup
    % Read data and save it to 'dataTable'
    \pgfplotstableread[
        col sep=comma,
    ]{testData.csv}\dataTable

    % Create second, in memory, table, that transposes the table, so that on the left side are the labels, followed by corresponding numbers, ordered by the date.
    % Save it to 'dataTransposed'
    \pgfplotstabletranspose[
        col sep=comma,
        string type,
        colnames from=day,
        input colnames to=day,
        columns={day, A, B, C}
    ]\dataTransposed{\dataTable}

    %%% Visualizing data
    % Plot data from 'dataTable'
    \begin{tikzpicture}
        \begin{axis}[
            width=\textwidth,
            height=\axisdefaultheight,
            xmin=01,
            xmax=31,
            ymin=0,
            ymax=180,
            xtick=data,
            xticklabel style={
                rotate=90,
                anchor=near xticklabel,
            },
            transpose legend,
            legend style={at={(0.5,-0.1)},anchor=north},
            legend columns=-1,
        ]

            \addplot table [col sep=comma,x=day,y=A] {\dataTable};
            \addplot table [col sep=comma,x=day,y=B] {\dataTable};
            \addplot table [col sep=comma,x=day,y=C] {\dataTable}; % the generated data

            \legend{A, B, C}
        \end{axis}
    \end{tikzpicture}      

    % Create tables from 'dataTransposed'
    \begin{center}  % Center table
        \pgfplotstabletypeset[
            col sep=comma,
            string type,
            % columns={day, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10},
            every head row/.style={
                before row=\toprule,
                after row=\midrule
            },
            every last row/.style={
                after row=\bottomrule
            }
        ]{\dataTransposed}
    \end{center}
\end{document}

Which produces the following output: enter image description here

As we can see the 'C' row is displayed in scientific numbers. How can I change that?

I've already tried to set the number format with:

\pgfkeys{/pgf/number format/int trunc}

[Within the preamble, document, before the table, before the data read etc.; Also as an argument within square brackets] Which did nothing?

Interestingly

\pgfmathprintnumber{5}

prints the number in the right number format, even without setting anything.

Thanks in advance.

3
  • 1
    You need the siunitx package for that. Commented Oct 27, 2018 at 15:42
  • @JohnKormylo Thanks for your help. Could you be so kind and give me an example on how to properly use siunitx with pgfplotstable? I just read into it but cannot find out how exactly I should do it ... I've included the package, and all suggested additional ones, and inserted the following setup: \sisetup{ round-mode = places, round-precision = 2, per-mode = symbol, group-four-digits = true, } \SendSettingsToPgf
    – Sakul6499
    Commented Oct 27, 2018 at 16:26
  • 2
    I stand corrected. All you have to do is replace string type with columns/day/.style={string type}. Commented Oct 27, 2018 at 18:01

1 Answer 1

3

As @JohnKormylo pointed out, replacing string type with columns/day/.style={string type} solves the problem.

The full, now working version:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Preamble
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use A4 paper
\documentclass[a4paper]{article}

% Use english language
\usepackage[english]{babel}
% Use UTF8 encoding
\usepackage[utf8]{inputenc}

% TikZ and PGF libraries for graphs
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}

% Booktabs for layout of tables
\usepackage{booktabs}

% Enable library for date-plotting
\usetikzlibrary{pgfplots.dateplot}

% Enable library for statistics [boxplot]
\usepgfplotslibrary{statistics}

% Always use newest version
\pgfplotsset{compat=newest,compat/show suggested version=false}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Pre - Document
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create data to test
\begin{filecontents}{testData.csv}
    day,        A,      B
    01,         10,     5
    02,         20,     10
    03,         100,    5
\end{filecontents}

% Create 'C' by calculating 'A - B'
\pgfplotstableset{
    create on use/C/.style={
        create col/expr={\thisrow{A}-\thisrow{B}},
    }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Document
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
    %%% Setup
    % Read data and save it to 'dataTable'
    \pgfplotstableread[
        col sep=comma,
    ]{testData.csv}\dataTable

    % Create second, in memory, table, that transposes the table, so that on the left side are the labels, followed by corresponding numbers, ordered by thedate.
    % Save it to 'dataTransposed'
    \pgfplotstabletranspose[
        col sep=comma,
        string type,
        colnames from=day,
        input colnames to=day,
        columns={day, A, B, C}
    ]\dataTransposed{\dataTable}

    %%% Visualizing data
    % Plot data from 'dataTable'
    \begin{tikzpicture}
        \begin{axis}[
            width=\textwidth,
            height=\axisdefaultheight,
            xmin=01,
            xmax=31,
            ymin=0,
            ymax=180,
            xtick=data,
            xticklabel style={
                rotate=90,
                anchor=near xticklabel,
            },
            transpose legend,
            legend style={at={(0.5,-0.1)},anchor=north},
            legend columns=-1,
        ]

            \addplot table [col sep=comma,x=day,y=A] {\dataTable};
            \addplot table [col sep=comma,x=day,y=B] {\dataTable};
            \addplot table [col sep=comma,x=day,y=C] {\dataTable}; % the generated data

            \legend{A, B, C}
        \end{axis}
    \end{tikzpicture}      

    % Create tables from 'dataTransposed'
    \begin{center}  % Center table
        \pgfplotstabletypeset[
            col sep=comma,
            columns/day/.style={string type},  % <--- HERE IS THE CHANGE
            % columns={day, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10},
            every head row/.style={
                before row=\toprule,
                after row=\midrule
            },
            every last row/.style={
                after row=\bottomrule
            }
        ]{\dataTransposed}
    \end{center}
\end{document}

Which results in: enter image description here

Thanks for helping out!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .