2

I'm trying to create a diagram with pgfplots. I have some data, which I'd like to have stored in the diagram code, alternatively (preferably not) in a separate file.

What I need to do is to calculate the cumulative percentage of the data. In other words, each data point is calculated by summing each of the numbers in the y column from the previous lines, dividing them by the sum of all numbers in the y column, and multiply by 100. For example, data point 3 would be calculated by (0 + 435 + 111 + 51) / 646 * 100 = 92%. (646 is the sum of all numbers in the y column)

Below are the numbers, and the diagram I'd like to create from them.

%x  y
0   0
1   435
2   111
3   51
4   23
5   10
6   5
7   5
8   3
9   3

Result wanted

Thank you very much in advance!

1

1 Answer 1

7

You can use the pgfplotstable package that ships with PGFPlots to calculate the cumulative percentage:

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage{filecontents}

\begin{filecontents*}{data.dat}
x  y
0   0
1   435
2   111
3   51
4   23
5   10
6   5
7   5
8   3
9   3
\end{filecontents*}

\begin{document}

% Read the data table into a macro
\pgfplotstableread{data.dat}\datatable

% Calculate the sum of the y column
\pgfmathsetmacro\pgfplotstablesum{0}
\pgfplotstableforeachcolumnelement{y}\of\datatable\as\yvalue{
    \pgfmathsetmacro\pgfplotstablesum{\pgfplotstablesum+\yvalue}
}

% Define a "virtual column" that calculates the cumulative percentage on the fly
\pgfplotstableset{
    create on use/cumulative percentage/.style={
        create col/expr={\pgfmathaccuma + 100*\thisrow{y}/\pgfplotstablesum}
    }
}

\begin{tikzpicture}
\begin{axis}[
    enlargelimits=false,
    axis lines*=left
]
\addplot [black, thick, mark=*] table [y=cumulative percentage] {\datatable} node {};
\end{axis}
\end{tikzpicture}
\end{document}

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.