Histograms

Introduction to Python for Finance

Adina Howe

Instructor

Why histograms for financial analysis?

histogram

Introduction to Python for Finance

Histograms and Data

  • Is your data skewed?
  • Is your data centered around the average?
  • Do you have any abnormal data points (outliers) in your data?
Introduction to Python for Finance

Histograms and matplotlib.pyplot

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=3)
plt.show()

hist1

Introduction to Python for Finance

Changing the number of bins

import matplotlib.pyplot as plt
plt.hist(prices, bins=6)
plt.show()

hist2

Introduction to Python for Finance

Normalizing histogram data

import matplotlib.pyplot as plt
plt.hist(prices, bins=6, normed=1)
plt.show()

normed

Introduction to Python for Finance

Layering histograms on a plot

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, normed=1)
plt.hist(x=prices_new, bins=6, normed=1)
plt.show()
Introduction to Python for Finance

Histogram result

two_hist_normed

Introduction to Python for Finance

Alpha: Changing transparency of histograms

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, normed=1, alpha=0.5)
plt.hist(x=prices_new, bins=6, normed=1, alpha=0.5)
plt.show()
Introduction to Python for Finance

Histogram result

two-hist-transparent

Introduction to Python for Finance

Adding a legend

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, normed=1, alpha=0.5, label="Prices 1")
plt.hist(x=prices_new, bins=6, normed=1, alpha=0.5, label="Prices New")
plt.legend()
plt.show()
Introduction to Python for Finance

Histogram result

pretty hist

Introduction to Python for Finance

Let's practice!

Introduction to Python for Finance

Preparing Video For Download...