https://www.amazon.com/Grammar-Graphics-Statistics-Computing/dp/0387245448
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]:
alt.Chart(customers).mark_point().encode(x='age', y='max speed', color='group')
Out[5]:
A bit more elaborateed
In [6]:
# https://altair-viz.github.io/user_guide/encoding.html
# https://altair-viz.github.io/user_guide/customization.html
alt.Chart(customers).mark_point().encode(
x='age',
y='max speed',
shape='group',
color=alt.Color('group',
type='nominal',
legend=alt.Legend(title="Type of Driver by Color"),
scale=alt.Scale(
range=['red', 'green', 'yellow'])),
).interactive()
Out[6]:
In [7]:
# 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
alt.layer(
background, highlight,
data=customers
).repeat(
column=["age", "max speed", "thousand km per year"]
)
Out[7]:
https://github.com/altair-viz/altair#design-approach-and-solution
In [ ]: