Bokeh Tutorial

Working with bokeh plotting library | March 01, 2017


  • python
  • bokeh

Bokeh is an excellent python package for interactive visualization in the web. I have been in the process of adopting it as my primary python plotting library. My favorite feature is creating dashboards using bokeh server. Here are a few examples. I will go through a few of the plotting features of bokeh in this post.

import pandas as pd
import pandas_datareader as pdd
import numpy as np
from math import pi

from bokeh.plotting import figure, show, output_notebook
tickers = ["AMZN", "MSFT", "GOOG", "FB", "TWTR", "^GSPC", "^DJI"]
start_date = "20160101"
end_date = "20161231"
data = pdd.get_data_yahoo(tickers, start_date, end_date)
output_notebook(hide_banner=True)

Scatter Chart

spx_dji = pd.DataFrame({"DJI": data[:, :, "^DJI"]["Adj Close"], "SPX": data[:, :, "^GSPC"]["Adj Close"]})
returns = spx_dji.pct_change().dropna()
from bokeh.charts import Scatter

scatter = Scatter(returns, x='SPX', y='DJI', title='SPX vs DJI', color="#539A9A",
                  plot_width=600, plot_height=400)

show(scatter)


volumes = data["Volume", :, ["AMZN", "MSFT", "GOOG", "FB"]]
returns = data["Adj Close", :, ["AMZN", "MSFT", "GOOG", "FB"]].pct_change()
volumes = volumes.stack().reset_index()
volumes.columns = ["Date", "Ticker", "Volume"]

returns = returns.stack().reset_index()
returns.columns = ["Date", "Ticker", "Returns"]
vr = pd.merge(volumes, returns).dropna()
p = figure(title = "Volume vs Returns",
           plot_width=600, plot_height=400)

p.xaxis.axis_label = 'Volume'
p.yaxis.axis_label = 'Returns'

p.circle(vr["Volume"], vr["Returns"],
         color=["#FFAEAE", "#84C2C2", "#DBF5A7", "#338181"], fill_alpha=0.2, size=10)

show(p)


Histogram

from bokeh.charts import Histogram, output_file, show

hist = Histogram(returns[returns.Ticker == "AMZN"], values="Returns",
                 title="Distribution by Returns", color="#B8E1E1", bins=25,
                 plot_width=600, plot_height=400)

show(hist)


Line

big_tech = data["Close", :, ["AMZN", "GOOG"]]
from bokeh.charts import Line, show, output_file

line = Line(big_tech, ylabel='Price', legend=True, color=['#539A9A', '#D75555'],
            plot_width=600, plot_height=400)

show(line)


Candlestick

selected = "^DJI"
df = data[:, :, selected]
df.reset_index(inplace=True)
df["Date"] = pd.to_datetime(df["Date"])

inc = df.Close > df.Open
dec = df.Open > df.Close
w = 12*60*60*1000
df = df[:20]
TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

p = figure(x_axis_type="datetime", tools=TOOLS, title = selected,
           plot_width=600, plot_height=400)
p.xaxis.major_label_orientation = pi/4
p.grid.grid_line_alpha = 0.3

p.segment(df.Date, df.High, df.Date, df.Low, color="black")
p.vbar(df.Date[inc], w, df.Open[inc], df.Close[inc], fill_color="#539A9A", line_color="black")
p.vbar(df.Date[dec], w, df.Open[dec], df.Close[dec], fill_color="#D75555", line_color="black")

show(p)

Phone

+1-312-566-7843

Address

------------------------
Chicago, IL 60605
United States of America