import holoviews as hv
from holoviews import opts
from holoviews.plotting.links import DataLink
hv.extension('bokeh')
import panel as pn
import panel.widgets as pnw
pn.extension()
print(f"holoviews version : {hv.__version__}")
print(f"panel version : {pn.__version__}")
holoviews version : 1.14.0 panel version : 0.10.2
from bokeh.sampledata.us_counties import data as counties
from bokeh.sampledata.unemployment import data as unemployment
# Generate dummy data
data2 = {}
data3 = {}
for ind, keys in enumerate(list(unemployment.keys())):
data2[keys] = unemployment[keys]+ind/10
data3[keys] = unemployment[keys]-ind/10
print(list(data2.items())[0:3])
[((1, 1), 9.7), ((1, 3), 9.2), ((1, 5), 13.6)]
unemployment_datasets={
"Unemployment": unemployment,
"Data2": data2,
"Data3": data3
}
def get_counties(var):
result = []
for cid, county in counties.items():
if county["state"]=="tx":
row = dict(county)
row[var]=unemployment_datasets[var][cid]
result.append(row)
return result
def plot_map(var='Unemployment'):
data = get_counties(var)
choropleth = hv.Polygons(data, ['lons', 'lats'], [('detailed name', 'County'), var])#, label='Texas ' + var)
choropleth.opts(opts.Polygons(width=400, height=400, tools=['hover', 'tap'], xaxis=None,
yaxis=None, color_index=var))
return choropleth
statistics = ("Unemployment", 'Data2', 'Data3')
# get a map object
layout_dict = {stat: plot_map(var=stat) for stat in statistics}
kdims = [hv.Dimension(('statistics', 'Statistics'))]
holomaps = hv.HoloMap(layout_dict, kdims=kdims)
layout_pn=pn.Column("#### HoloMaps", holomaps, "#### Data2", plot_map(var="Data2")).servable()
holomaps
from bokeh.resources import INLINE
hv.save(holomaps,'test_holomap.html', resources=INLINE)
layout_pn
# save panel object
layout_pn.save('test_panel.html', embed=True)
unemployment_datasets={
"Unemployment": unemployment,
"Data2": data2,
"Data3": data3
}
def get_counties(var):
result = []
for cid, county in counties.items():
if county["state"]=="tx":
row = dict(county)
row[var]=unemployment_datasets[var][cid]
result.append(row)
return result
def plot_map(var='Unemployment'):
data = get_counties(var)
choropleth = hv.Polygons(data, ['lons', 'lats'], [('detailed name', 'County'), var])#, label='Texas ' + var)
choropleth.opts(opts.Polygons(width=400, height=400, tools=['hover', 'tap'], xaxis=None,
yaxis=None, color_index=var))
return choropleth
statistics = ("Unemployment", 'Data2', 'Data3')
# get a map object
layout_dict = {stat: plot_map(var=stat) for stat in statistics}
kdims = [hv.Dimension(('statistics', 'Statistics'))]
holomaps = hv.HoloMap(layout_dict, kdims=kdims)
def get_table(var='Unemployment'):
data = get_counties(var)
table = hv.Table(data, [('detailed name', 'County'), var])
table.opts(
opts.Table(height=328)
)
return table
variable = pnw.RadioButtonGroup(name='variable', value='Unemployment',
options=list(statistics))
@pn.depends(variable)
def reactive_map(variable):
return plot_map(variable)
@pn.depends(variable)
def reactive_table(variable):
return get_table(variable)
widgets = pn.Column("<br>\n# Statistics", variable)
# maps = pn.Column(widgets,pn.Row(reactive_map, reactive_table))
maps_pn = pn.Column(widgets,pn.Row(reactive_map, reactive_table))
maps_pn.save('maps_panel.html', embed=True)
maps_pn
maps_pn.save('maps_panel.html', embed=True)
import pandas as pd; import numpy as np; import matplotlib.pyplot as plt
data = pd.read_csv('datatraining.txt')
data['date'] = data.date.astype('datetime64[ns]')
data = data.set_index('date')
data.tail()
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvas
%matplotlib inline
def mpl_plot(avg, highlight):
fig = Figure()
FigureCanvas(fig) # not needed in mpl >= 3.1
ax = fig.add_subplot()
avg.plot(ax=ax)
if len(highlight): highlight.plot(style='o', ax=ax)
return fig
def find_outliers(variable='Temperature', window=30, sigma=10, view_fn=mpl_plot):
avg = data[variable].rolling(window=window).mean()
residual = data[variable] - avg
std = residual.rolling(window=window).std()
outliers = (np.abs(residual) > std * sigma)
return view_fn(avg, avg[outliers])
import panel as pn
pn.extension()
fig_pn=pn.interact(find_outliers)
fig_pn
fig_pn.save('fig_panel.html', embed=True)