I have the following data:

data = {{0, 0}, {20, 1.4}, {25, 9.8}, {30, 32.2}, {35, 38.2}, {40, 15.6}, {45, 2.7}, {50, 0.1}};

where the second coordinates are frequencies measured as percent of total (adding, therefore, to $100$). I would like to produce a cumulative frequency plot. What would be the neatest way to do so?

  • 3
    $\begingroup$ I'm not sure whether you're correct describing your data as frequencies. I'm assuming you're using this phrase in a statistical context, meaning counts and not a physical context (in Hertz). Given you have cumulative frequency I suppose it's the former, but then how can you have non-integer values? $\endgroup$ – Sjoerd C. de Vries Mar 10 '13 at 21:37
  • $\begingroup$ I meant accumulative. I was looking for the answer you gave me below, thanks! $\endgroup$ – Frederik Brinck Jensen Mar 11 '13 at 7:10
  • $\begingroup$ I didn't say "cumulative" was wrong. I just made a remark about the values not being integer. I now assume they are relative frequencies and that therefore there's no reason for the term 'Total' in my answer (it does no harm either). $\endgroup$ – Sjoerd C. de Vries Mar 11 '13 at 9:08
  • $\begingroup$ I didn't get that they were relative frequencies, however seems that you're right about that! $\endgroup$ – Frederik Brinck Jensen Mar 11 '13 at 12:50
  • $\begingroup$ There still remain questions of the meaning and interpretation of these data, Frederik. E.g., does the appearance of {{0,0}, {20,1.4}, ...} mean that $1.4$% of the frequency lies within the interval $[0,20]$ or that $1.4$% lies *exactly* at $20$? Should the plot reflect the data accurately or--as suggested by the accepted answer--attempt to interpolate between the bin cutpoints? Evidently the interpolation must be monotonic, but should it necessarily be linear (as in the accepted answer)? $\endgroup$ – whuber Mar 11 '13 at 17:20
ListLinePlot[{data[[All, 1]], Accumulate[#]/Total[#] &@data[[All, 2]]}\[Transpose]]

Mathematica graphics

  • $\begingroup$ you probably meant Accumulate...data[[All,2]]? $\endgroup$ – kglr Mar 11 '13 at 3:56
  • $\begingroup$ @kguler yeah, will correct figure when I'm near mma. Thanks. $\endgroup$ – Sjoerd C. de Vries Mar 11 '13 at 6:35
  • 1
    $\begingroup$ Terse style (in the FrontEnd): ListLinePlot[{#, Accumulate@#/Tr@# &@#2}\[Transpose]] & @@ (data\[Transpose]) $\endgroup$ – Mr.Wizard Mar 11 '13 at 8:08

You can transform the data as follows, and then plot transdata,

transdata = Partition[Riffle[data[[All, 1]], Accumulate@data[[All, 2]]], 2]

Surely this is a neat way, but it remains to be seen if its the neatest.

  • $\begingroup$ Also # ~Riffle~ Accumulate[#2] ~Partition~ 2 & @@ Transpose[data], though I would use data[[All, 2]] = Accumulate @ data[[All, 2]]; data :-) $\endgroup$ – Mr.Wizard Mar 11 '13 at 3:34
  • 2
    $\begingroup$ also, transdata = Transpose[{data[[All, 1]], Accumulate@data[[All, 2]]}] $\endgroup$ – Iiss Mar 11 '13 at 4:35
  • 1
    $\begingroup$ Yes. This one looks good in the FrontEnd: {#, Accumulate@#2}\[Transpose] & @@ (data\[Transpose]) (By the way the only reason I haven't voted for this answer is that I honestly don't understand the question.) $\endgroup$ – Mr.Wizard Mar 11 '13 at 4:59
  • $\begingroup$ @Mr.Wizard I have performed a somewhat aggressive edit of the question to reflect an interpretation suggested by the accepted answer. $\endgroup$ – whuber Mar 11 '13 at 17:17

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.