In [1]:
import altair as alt
# remove when running on Colab or JupyterLab / add when running in a jupyter notebook
alt.renderers.enable('notebook')
Out[1]:
In [2]:
# Choose one of the two following data sets, the larger one gives better results, but might clutter the visualization depending on resolution
!curl -O https://raw.githubusercontent.com/DJCordhose/ai/master/notebooks/scipy/data/insurance-customers-1500.csv
# !curl -O https://raw.githubusercontent.com/DJCordhose/ai/master/notebooks/scipy/data/insurance-customers-300.csv
In [3]:
import pandas as pd
# customers = pd.read_csv('./insurance-customers-300.csv', sep=';')
customers = pd.read_csv('./insurance-customers-1500.csv', sep=';')
In [4]:
customers.head()
Out[4]:
In [5]:
# https://altair-viz.github.io/gallery/interactive_layered_crossfilter.html
import altair as alt
brush = alt.selection(type='interval', encodings=['x'], resolve='intersect', empty='none')
# Define the base chart, with the common parts of the
# background and highlights
base = alt.Chart().mark_bar().encode(
x=alt.X(alt.repeat('column'), type='quantitative'),
y='count()'
).properties(
width=250,
height=150
)
# blue background with selection
background = base.properties(
selection=brush
)
# yellow highlights on the transformed data
highlight = base.encode(
color=alt.value('goldenrod')
).transform_filter(
brush
)
# layer the two charts & repeat
chart = alt.layer(
background, highlight,
data=customers
).repeat(
column=["age", "max speed", "thousand km per year"]
)
chart
Out[5]:
In [6]:
# save the complete, fully functional html page
chart.save('chart.html')
In [7]:
# just save the vega-lite json
chart.save('chart.json')