Altair¶%matplotlib inline
# standard
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# I've got style,
# miles and miles
import seaborn as sns
sns.set()
sns.set_context('notebook', font_scale=1.5)
cp = sns.color_palette()
from altair import *
ts = pd.read_csv('data/ts.csv')
# casting to datetime is important for
# ensuring plots "just work"
ts = ts.assign(dt = pd.to_datetime(ts.dt))
ts.head()
# in matplotlib-land, the notion of a "tidy"
# dataframe matters not
dfp = ts.pivot(index='dt', columns='kind', values='value')
dfp.head()
c = Chart(ts).mark_line().encode(
x='dt',
y='value',
color='kind'
)
c
c = Chart(ts).mark_line().encode(
x='dt',
y='value',
color=Color('kind', scale=Scale(range=cp.as_hex()))
)
c
df = pd.read_csv('data/iris.csv')
df.head()
c = Chart(df).mark_point(filled=True).encode(
x='petalLength',
y='petalWidth',
color='species'
)
c
c = Chart(ts).mark_line().encode(
x='dt',
y='value',
color='kind',
column='kind'
)
c.configure_cell(height=200, width=200)
c = Chart(df).mark_point().encode(
x='petalLength',
y='petalWidth',
color='species',
column=Column('species',
title='Petal Width v. Length by Species')
)
c.configure_cell(height=300, width=300)
tmp_n = df.shape[0] - df.shape[0]/2
df['random_factor'] = (np.\
random.\
permutation(['A'] * tmp_n +
['B'] * (df.shape[0] - tmp_n)))
df.head()
c = Chart(df).mark_point().encode(
x='petalLength',
y='petalWidth',
color='species',
column=Column('species',
title='Petal Width v. Length by Species'),
row='random_factor'
)
c.configure_cell(height=200, width=200)
# please note: this code is super speculative -- I'm
# assuming there's a better way to do this and I just
# don't know it
c = Chart(df).mark_point(opacity=.5).encode(
x='species',
y='petalWidth'
)
c25 = Chart(df).mark_tick(tickThickness=3.0,
tickSize=20.0,
color='r').encode(
x='species',
y='q1(petalWidth)'
)
c50 = Chart(df).mark_tick(tickThickness=3.0,
tickSize=20.0,
color='r').encode(
x='species',
y='median(petalWidth)'
)
c75 = Chart(df).mark_tick(tickThickness=3.0,
tickSize=20.0,
color='r').encode(
x='species',
y='q3(petalWidth)'
)
LayeredChart(data=df, layers=[c, c25, c50, c75])
c = Chart(df).mark_bar(opacity=.75).encode(
x=X('petalWidth', bin=Bin(maxbins=30)),
y='count(*)',
color=Color('species', scale=Scale(range=cp.as_hex()))
)
c
df = pd.read_csv('data/titanic.csv')
df.head()
dfg = df.groupby(['survived', 'pclass']).agg({'fare': 'mean'})
dfg
died = dfg.loc[0, :]
survived = dfg.loc[1, :]
c = Chart(df).mark_bar().encode(
x='survived:N',
y='mean(fare)',
color='survived:N',
column='class')
c.configure(
facet=FacetConfig(cell=CellConfig(strokeWidth=0, height=250))
)