Ben Chuanlong Du's Blog

It is never too late to learn.

Overlay Plots in HoloViews

In [1]:
import numpy as np
import pandas as pd
import holoviews as hv

hv.extension("bokeh")
In [61]:
macro_df = pd.read_csv("http://assets.holoviews.org/macro.csv", "\t")
key_dimensions = [("year", "Year"), ("country", "Country")]
value_dimensions = [
    ("unem", "Unemployment"),
    ("capmob", "Capital Mobility"),
    ("gdp", "GDP Growth"),
    ("trade", "Trade"),
]
macro = hv.Table(macro_df, key_dimensions, value_dimensions)
In [62]:
%%opts Scatter [width=700 height=400 scaling_method='width' scaling_factor=2 size_index=2 show_grid=True tools=['hover']] 
%%opts Scatter (color=Cycle('Category20') line_color='k' muted_alpha=0.01)
%%opts NdOverlay [legend_position='left' show_frame=False]
gdp_unem_scatter = macro.to.scatter('Year', ['GDP Growth', 'Unemployment'])
gdp_unem_scatter.overlay('Country')
Out[62]:
In [63]:
%%opts Overlay [width=700 height=400 show_frame=False]
%%opts Curve (color='k') Scatter [color_index=2 size_index=2 scaling_factor=1.4] (cmap='Blues' line_color='k')
%%opts VLine (color='k' line_width=1)
%%opts Text (text_font_size='13px')
gdp_curves = macro.to.curve('Year', 'GDP Growth')
gdp_unem_scatter = macro.to.scatter('Year', ['GDP Growth', 'Unemployment'])
annotations = hv.Arrow(1973, 8, 'Oil Crisis', 'v') * hv.Arrow(1975, 6, 'Stagflation', 'v') *\
hv.Arrow(1979, 8, 'Energy Crisis', 'v') * hv.Arrow(1981.9, 5, 'Early Eighties\n Recession', 'v')
gdp_curves * gdp_unem_scatter* annotations
Out[63]:
In [64]:
xs = [0.1 * i for i in range(100)]
curve_list = [hv.Curve((xs, [np.sin(f * x) for x in xs])) for f in [0.5, 0.75]]
scatter_list = [hv.Scatter((xs[::5], f * np.linspace(0, 1, 20))) for f in [-0.5, 0.5]]
overlay = hv.Overlay(curve_list + scatter_list)
overlay
Out[64]:
In [65]:
from scipy.integrate import odeint

sigma = 10
rho = 28
beta = 8.0 / 3
theta = 3 * np.pi / 4


def lorenz(xyz, t):
    x, y, z = xyz
    x_dot = sigma * (y - x)
    y_dot = x * rho - x * z - y
    z_dot = x * y - beta * z
    return [x_dot, y_dot, z_dot]


initial = (-10, -7, 35)
t = np.arange(0, 100, 0.006)

solution = odeint(lorenz, initial, t)

x = solution[:, 0]
y = solution[:, 1]
z = solution[:, 2]
xprime = np.cos(theta) * x - np.sin(theta) * y

lorenzian = hv.Overlay(
    [hv.Path(d) for d in zip(np.array_split(xprime, 7), np.array_split(z, 7))]
)
In [66]:
lorenzian.options("Path", color=hv.Palette("Blues"))
Out[66]:
In [ ]:
 

Comments