A histogram is a graph that shows the distribution of numerical data. The matplotlib
Python library makes creating a histogram a snap. Here's how.
Before using this recipe, familiarize yourself with the following recipes as we'll be building on them:
Creating a Pandas DataFrame from a MongoDB query
Generating a frequency table for a single column by date
To create a histogram for a single column in a Pandas DataFrame, begin by importing all the required libraries. To show
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
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, ...