Simple Plotting using Altair

https://altair-viz.github.io/


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]:
RendererRegistry.enable('notebook')

Car insurance customer data


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


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 26935  100 26935    0     0  78299      0 --:--:-- --:--:-- --:--:-- 78299
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  5421  100  5421    0     0  23166      0 --:--:-- --:--:-- --:--:-- 23166

In [2]:
import pandas as pd
customers = pd.read_csv('./insurance-customers-300.csv', sep=';')
# customers = pd.read_csv('./insurance-customers-1500.csv', sep=';')

In [3]:
customers.head()


Out[3]:
max speed age thousand km per year group
0 167.0 41.0 5.0 1
1 158.0 18.0 15.0 1
2 147.0 28.0 14.0 2
3 161.0 43.0 62.0 2
4 179.0 36.0 42.0 0

A quick interactive overview


In [4]:
alt.Chart(customers).mark_point().encode(
    x='age', 
    y='max speed', 
    color='group')


Out[4]:

A bit more elaborateed


In [5]:
# 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[5]: