Histograms in Python

In this example, I am going to use a dataset from Kaggle on house sales in King County, USA (URL: https://www.kaggle.com/harlfoxem/housesalesprediction; accessed 28th August 2016).

Creating a histogram:


Simple histogram of the frequency of different house prices.

Specifying the number of bins

You can easily specify the number of bins by passing the argument:

bins = number

Here I use 50 bins.
Histogram with 50 bins.

Changing the range

You can change the range by passing the argument of:

range = [min_number, max_number]

In my example, I want to visualise only the prices from 0 to 2,000,000.

House prices with a range of dates specified

Adding histogram titles and axis names

Adding the title of the histogram:

Naming the x axis:

Naming the y axis:

Title, x and y axis titles included

Changing the orientation

The default orientation of the histogram is vertical. You can change it to horizontal by passing the argument:

orientation = ‘horizontal’

Horizontal orientation of the histogram

Changing the transparency

You can change how transparent the histogram is by adding the argument ‘alpha’ with values between 0 to 1. 1 is the default value.

Example 1:

Transparency of 0.3

Example 2:

Transparency of 0.7

Changing colours

Changing colours requires the ‘color’ argument.

Pink histogram

Code on Github: https://github.com/kasiarachuta/Blog/blob/master/Histograms%20in%20Python.ipynb