1
$\begingroup$

I have a list: {1,2,2,3,3,3,3,4,4,5}. I want to plot the percentage occurrence of elements in the whole list.

So the list plot I want to do is:

list={{1,1/10},{2,2/10},{3,4/10},{4,2/10},{5,1/10}};
ListPlot[list]

What is the name of this kind of plot in Mathematica?

How should I do it in Mathematica 7?

$\endgroup$
  • $\begingroup$ What type of plot do you want? Like a bar graph? $\endgroup$ – DanZimm May 23 '14 at 18:25
  • $\begingroup$ Yes. Like a bar graph. $\endgroup$ – mike May 23 '14 at 18:37
  • $\begingroup$ @wolfies How do I move it? $\endgroup$ – mike May 23 '14 at 18:46
5
$\begingroup$
data = {1, 2, 2, 3, 3, 3, 3, 4, 4, 5};
dist = EmpiricalDistribution[data];

DistributionDomain[dist]
(* {1, 2, 3, 4, 5} *)

dist["Weights"]
(* {1/10, 1/5, 2/5, 1/5, 1/10} *)

Transpose[{dist["Domain"], dist["Weights"]}]
(* {{1, 1/10}, {2, 1/5}, {3, 2/5}, {4, 1/5}, {5, 1/10}} *)

Properties

{#, {dist[#]}} & /@ dist["Properties"] // TableForm 

enter image description here

PMF

Table[{x, PDF[dist, x]}, {x, 0, 6, .5}] // 
   Transpose // TableForm[#, TableHeadings -> {{x, PDF[x]}, None}] & // Style[#, 16] &

enter image description here

Plotting with ListPlot

ListPlot[Transpose[{dist["Domain"], dist["Weights"]}],
    PlotStyle -> PointSize[Large], ImageSize -> 500, PlotRange -> {0, .5},
    Filling -> Axis,  FillingStyle -> Directive[Opacity[.5], Blue, Thickness[.01]]]

enter image description here

Plotting with DiscretePlot

DiscretePlot[PDF[dist, x], {x, DistributionDomain[dist]},
    PlotStyle -> PointSize[Large], ImageSize -> 500, 
    FillingStyle -> Thickness[.01], PlotRange -> {0, .5}]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ excellent! but you should automate the "6" $\endgroup$ – eldo Jun 4 '14 at 13:11
  • $\begingroup$ @eldo, thank you.. done.. $\endgroup$ – kglr Jun 4 '14 at 13:21
5
$\begingroup$

Given:

lis = {1, 2, 2, 3, 3, 3, 3, 4, 4, 5}
 m  = Length[lis];

... then:

ListPlot[ Map[ {#[[1]], #[[2]]/m} &, Tally[lis]], Filling -> Axis]

is a correct representation of the discrete pmf:


(source: org.au)

If you have the mathStatica add-on for Mathematica, one can do this even more simply with:

FrequencyPlotDiscrete[lis, 1] 


(source: org.au)

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks again. This is exactly what I want. $\endgroup$ – mike May 23 '14 at 18:57
3
$\begingroup$
Histogram[{1, 2, 2, 3, 3, 3, 3, 4, 4, 5}, {1}, "Probability"]

Or how about this:

BarChart[#[[2]]/Total[#[[2]]], ChartLabels -> #[[1]]] &[Transpose[Tally[{1, 2, 2, 3, 3, 3, 3, 4, 4, 5}]]]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Can't agree with this. A histogram (as provided) shows a continuous mass from 1 to 2, and from 2 to 3 etc ... whereas the problem at hand is discrete, with a discrete mass at the points x= {1,2,3,4,5}, and nothing in between them. $\endgroup$ – wolfies May 23 '14 at 18:36
  • $\begingroup$ @heropup Thanks for the answer. I tried your method and it worked OK. wolfies is right, I want to have all the density concentrated at the discrete points. $\endgroup$ – mike May 23 '14 at 18:45

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.