Calculate the column sum in an expression when plotting a table

pgfplotspgfplotstable

I have some data as a table, as seen in the code below. The second column is the y-axis. The entire column sums to 100. For each y value being plotted, I'd like to plot y_value / y_total e.g. 10 / 100, 20 / 100, etc.

I've tried to do this using y expr, but it's only limited to accessing columns in the current row. It doesn't seem ideal to calculate the sum repeatedly for each column anyway. Ideally, the column can be summed once, and then I can start plotting using y expr with sum I calculated beforehand.

I've seen a way to calculate the column sum by transposing the table and then transposing it back, but the sum is stored in a single row, which I cannot always access in y expr.

How can I do this?

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}

\pgfplotstableread{
  1 10
  2 20
  3 30
  4 40
}{\data}

\begin{tikzpicture}
  \begin{axis}
    % The 100 is hard-coded currently.
    \addplot table[y expr=\thisrow{1}/100] {\data};
  \end{axis}
\end{tikzpicture}

\end{document}

Best Answer

So you are after something like the following ... For details please have a look at the comments in the code.

% used PGFPlots v1.18.1
\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
        \pgfplotstableread{
          x y
          1 20
          2 40
          3 60
          4 80
        }{\loadedtable}

        % add a column in the table summing up the values % accumulatively
        % with the sum of all values one can calculate the percentages of the values
        \pgfplotstablecreatecol[
            expr accum={
                round(\pgfmathaccuma) + \thisrow{y}
            }{0}
        ]{sum}{\loadedtable}

            % get the number of data rows of the loaded table
            \pgfplotstablegetrowsof{\loadedtable}
                \pgfmathsetmacro{\LastRow}{\pgfplotsretval-1}
            % now get the last entry of the «sum» column which contains the
            % sum of the values
            \pgfplotstablegetelem{\LastRow}{sum}\of{\loadedtable}
                \pgfmathsetmacro{\Sum}{\pgfplotsretval}
    \begin{axis}
        \addplot table[x=x,y expr=\thisrow{y}/\Sum] {\loadedtable};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code