Jupyter Snippet CB2nd 06_altair
Jupyter Snippet CB2nd 06_altair
6.6. Creating plots with Altair and the Vega-Lite specification
import altair as alt
alt.list_datasets()
['airports',
...
'driving',
'flare',
'flights-10k',
'flights-20k',
'flights-2k',
'flights-3m',
'flights-5k',
'flights-airport',
'gapminder',
...
'wheat',
'world-110m']
df = alt.load_dataset('flights-5k')
df.head(3)
alt.Chart(df).mark_point().encode(
x='date',
y='delay',
size='distance',
)
df_la = df[df['origin'] == 'LAX']
x = alt.X('date', bin=True)
y = 'average(delay)'
alt.Chart(df_la).mark_bar().encode(
x=x,
y=y,
)
sort_delay = alt.SortField(
'delay', op='average', order='descending')
x = alt.X('origin', sort=sort_delay)
y = 'average(delay)'
alt.Chart(df).mark_bar().encode(
x=x,
y=y,
)
{
"$schema": "https://vega.github.io/schema/vega-lite/v1.2.1.json",
"data": {
"values": [
{
"date": "2001-01-10 18:20:00",
"delay": 25,
"destination": "HOU",
"distance": 192,
"origin": "SAT"
},
...
]
},
"encoding": {
"x": {
"field": "origin",
"sort": {
"field": "delay",
"op": "average",
"order": "descending"
},
"type": "nominal"
},
"y": {
"aggregate": "average",
"field": "delay",
"type": "quantitative"
}
},
"mark": "bar"
}