直方圖

Python 金融入門

Adina Howe

Professor

為何在財務分析中用直方圖?

直方圖

Python 金融入門

直方圖與資料

  • 你的資料有偏態嗎?
  • 資料是否集中在平均附近?
  • 是否有異常值(outliers)?
Python 金融入門

直方圖與 matplotlib.pyplot

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

hist1

Python 金融入門

調整箱數(bins)

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

hist2

Python 金融入門

正規化直方圖資料

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

normed

Python 金融入門

在同一圖上疊加直方圖

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, density=True)
plt.hist(x=prices_new, bins=6, density=True)
plt.show()
Python 金融入門

直方圖結果

兩個正規化直方圖

Python 金融入門

Alpha:調整直方圖透明度

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, density=True, alpha=0.5)
plt.hist(x=prices_new, bins=6, density=True, alpha=0.5)
plt.show()
Python 金融入門

直方圖結果

兩個半透明直方圖

Python 金融入門

加入圖例

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, density=True, alpha=0.5, label="Prices 1")
plt.hist(x=prices_new, bins=6, density=True, alpha=0.5, label="Prices New")
plt.legend()
plt.show()
Python 金融入門

直方圖結果

美化後直方圖

Python 金融入門

一起來練習吧!

Python 金融入門

Preparing Video For Download...