# Plot the probability mass function

I am trying to plot the probability mass function of a sample of a discrete metric.

If it was continuous, I know that using pandas it would be as simple as calling:

sample.plot(kind="density")


But I'm afraid that this is not enough (or not right) for my sample. Is there a function within matplotlib, scipy, numpy, etc. that I could use for plotting it?

• Please explain what would be "right" for your sample. Perhaps you could post a sample figure? – whuber Aug 11 '14 at 17:02
• My sample is for a discrete variable and density plots are for continuos variables. Here's what I get using the code above: dropbox.com/s/3kr6l7lub2wxx55/… As I am dealing with a discrete sample, I don't think I should get a continuos plot. – Milena Araujo Aug 11 '14 at 17:08
• But what kind of a plot do you wish to produce? What do you hope to learn from this plot or convey about the sample to other people? There are many, many different ways to construct a graphical display of a sample! – whuber Aug 11 '14 at 17:10
• I want to learn about it's shape, I am trying to infer this sample's distribution (or the one that it looks like the best). I've learned that PMFs are the discrete version of PDFs, and the later helps to infer a sample's distribution. – Milena Araujo Aug 11 '14 at 17:16
• Are these results ordered categories, or are they a set of discrete values on a lattice (such as counts), or something else? – Glen_b Aug 11 '14 at 17:59

There are two parts to your question - how to display discrete data (a data visualization issue) and how to do it in Python (a "what function do I call" issue).

I will deal with the first one.

With discrete distributions, there are a number of possible ways to display data.

Leaving aside direct implementation issues for the present, I see three main competitors:

1. the empirical cdf.

2. a sample probability function.

These are quite suitable for count data, for example.

3. a barplot.

This is quite suitable for ordered categories. If you order the bars from largest to smallest (or in some other meaningful-to-your-needs fashion), it's also suitable for unordered categories.

There are numerous other possibilities. However, I don't think a histogram is generally suitable for discrete data, especially not one where the bins are automatically chosen. The first problem is that a histogram density estimate uses area rather than height to convey relative probabilities, so it fairly directly conveys an impression of continuity. The second issue is with bin-width -- you need to choose it carefully or you may be doing things like having alternating bins either combining two categories or one, or perhaps having a smaller or larger gap between two categories than between the others (often an end-category):

As we see the gaps are not of constant width, throwing off the impression the plot conveys.

As for how you do things like this in python, after you choose a display, that would probably be a good, more specific question (but probably more on topic elsewhere; worded right it might fit better on StackOverflow, but you should check their help for what's on topic. With careful phrasing it might survive here, or it might work on Superuser.

• Is this "sample probability function" the same as the "probability mass function" ? They do look similar. If not, what's the difference between them ? – Milena Araujo Aug 11 '14 at 20:55
• In my post "probability function" is synonymous with "probability mass function", though the term is sometimes applied to continuous random variables to refer to their density function (see here for example). The word "sample" applies to both, since you're dealing with a sample. – Glen_b Aug 11 '14 at 22:36
• @Glen_b Thank you for the explanation. I have been struggling with similar kind of issue. I suppose plotting a PMF will give more insights about the data. – Sitz Blogz Apr 21 '16 at 16:39
• very nice plot! – Belter Aug 22 '17 at 13:27