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:


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?

  • 3
    $\begingroup$ Please explain what would be "right" for your sample. Perhaps you could post a sample figure? $\endgroup$
    – whuber
    Commented Aug 11, 2014 at 17:02
  • $\begingroup$ 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. $\endgroup$ Commented Aug 11, 2014 at 17:08
  • $\begingroup$ 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! $\endgroup$
    – whuber
    Commented Aug 11, 2014 at 17:10
  • $\begingroup$ 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. $\endgroup$ Commented Aug 11, 2014 at 17:16
  • 1
    $\begingroup$ Are these results ordered categories, or are they a set of discrete values on a lattice (such as counts), or something else? $\endgroup$
    – Glen_b
    Commented Aug 11, 2014 at 17:59

1 Answer 1


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.

    enter image description here

  2. a sample probability function.

    enter image description here

    These are quite suitable for count data, for example.

  3. a barplot.

    enter image description here

    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):

Histogram with smaller gap between 0 and 1 category and others

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.

  • $\begingroup$ Is this "sample probability function" the same as the "probability mass function" ? They do look similar. If not, what's the difference between them ? $\endgroup$ Commented Aug 11, 2014 at 20:55
  • 2
    $\begingroup$ 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. $\endgroup$
    – Glen_b
    Commented Aug 11, 2014 at 22:36
  • $\begingroup$ @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. $\endgroup$
    – Sitz Blogz
    Commented Apr 21, 2016 at 16:39
  • $\begingroup$ very nice plot! $\endgroup$
    – Belter
    Commented Aug 22, 2017 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.