Stocks example


Most examples work across multiple plotting backends, this example is also available for:

In [1]:
import numpy as np
import pandas as pd
import holoviews as hv
%output fig='svg' dpi=120

Defining the data

In [2]:
from holoviews.operation.timeseries import rolling
from bokeh.sampledata.stocks import AAPL, GOOG, IBM, MSFT

def get_curve(data, label=''):
    df = pd.DataFrame(data)
    df['date'] ='datetime64[ns]')
    return hv.Curve(df, ('date', 'Date'), ('adj_close', 'Price'), label=label)

hv.Dimension.type_formatters[np.datetime64] = '%Y'

aapl = get_curve(AAPL, label='AAPL')
goog = get_curve(GOOG, label='GOOG')
ibm  = get_curve(IBM, label='IBM')
msft = get_curve(MSFT, label='MSFT')

avg_curve = rolling(aapl, rolling_window=30).relabel('Average')
avg_scatter = hv.Scatter(avg_curve, label='close')

color_cycle = hv.Cycle(values=['#A6CEE3', '#B2DF8A','#33A02C', '#FB9A99'])


In [3]:
plot_opts = dict(aspect=1, fig_size=200, legend_position='top_left')

hv.Store.options(backend='matplotlib').Curve = hv.Options('style', color=color_cycle)

scatter_style = dict(alpha=0.2, size=4, color='darkgrey')
curve_style = dict(color='navy')

(aapl * goog * ibm * msft).opts(plot=plot_opts) +\
(avg_scatter(style=scatter_style) * avg_curve(style=curve_style)).opts(plot=plot_opts)

Download this notebook from GitHub (right-click to download).