Another interesting plot that we can create is one showing cumulative distribution. This plot shows the probability of finding a number in a bin or any lower bin. We do this by adding a single argument to the hist()
function.
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
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)
accidents = pd.DataFrame(list(data))
plt.hist(casualty_count['Number_of_Casualties'], bins=20, normed=True, cumulative=True) plt.title('Cumulative Distribution) plt.xlabel('Value') plt.ylabel('Frequency') plt.show()
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=20, normed=True, cumulative=True)
With the addition of cumulative=True
, we turn the histogram into a cumulative distribution plot, and see the following plot as a result: