Plotting the data as a probability distribution

Plotting our data in a histogram as a probability distribution tells matplotlib to integrate the total area of the histogram, and scale the values appropriately. Rather than showing how many values go into each bin as in the previous recipe, we'll have the probability of finding a number in the bin.

How to do it…

  1. To create a probability distribution for a single column in a Pandas DataFrame, begin by importing all the required libraries. To show the matplotlib plots in IPython Notebook, we will use an IPython magic function which starts with %:
    %matplotlib inline
    import pandas as pd
    import numpy as np
    from pymongo import MongoClient
    import matplotlib as mpl
    import matplotlib.pyplot as plt
  2. Next, connect to MongoDB, and run a query specifying the five fields to be retrieved from the MongoDB data:
    client = MongoClient('localhost', 27017)
    db = client.pythonbicookbook
    collection = db.accidents
    fields = {'Date':1,
              'Police_Force':1,
              'Accident_Severity':1,
              'Number_of_Vehicles':1,
              'Number_of_Casualties':1}
    data = collection.find({}, fields)
  3. Next, create a DataFrame from the results of the query:
    accidents = pd.DataFrame(list(data))
  4. Finally, create the probability histogram and render it inline. This histogram shows the probability of finding a number in a bin:
    plt.hist(casualty_count['Number_of_Casualties'],
             bins=30,
             normed=True)
    plt.title('Probability Distribution')
    plt.xlabel('Value')
    plt.ylabel('Probability')
    plt.show()

How it works…

This recipe works exactly like the previous recipe with the exception of the way we create the histogram:

plt.hist(casualty_count['Number_of_Casualties'],
         bins=30,
         normed=True)

With the addition of normed=True, we turn the histogram into a probability distribution, and see the following plot as a result:

How it works…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset