Altair Gallery
Altair Gallery
[1]: DataTransformerRegistry.enable('default')
1 Altair Gallery
1.1 Simple Charts
1.1.1 Simple Bar Chart
alt.Chart(…)
alt.Chart(source).mark_rect().encode(
1
x='x:O',
y='y:O',
color='z:Q'
).properties(width=400, height=400).display(renderer='svg')
alt.Chart(…)
alt.Chart(source).mark_bar().encode(
alt.X("IMDB_Rating:Q", bin=True),
y='count()',
).properties(width=500, height=300).display(renderer='svg')
alt.Chart(…)
[5]: x = np.arange(100)
source = pd.DataFrame({
'x': x,
'f(x)': np.sin(x / 5)
})
alt.Chart(source).mark_line().encode(
x='x',
y='f(x)'
).properties(width=500, height=300).display(renderer='svg')
alt.Chart(…)
alt.Chart(source).mark_bar().encode(
x='year:O',
y="wheat:Q",
# The highlight will be set on the result of a conditional statement
color=alt.condition(
alt.datum.year == 1810, # If the year is 1810 this test returns True,
alt.value('orange'), # which sets the bar orange.
alt.value('steelblue') # And if it's not true it sets the bar␣
,→steelblue.
2
).properties(width=600).display(renderer='svg')
alt.Chart(…)
bars = alt.Chart(source).mark_bar().encode(
x='wheat:Q',
y="year:O"
)
text = bars.mark_text(
align='left',
baseline='middle',
dx=5 # Nudges text to right so it doesn't appear on top of the bar
).encode(
text='wheat:Q'
)
(bars + text).properties(height=900).display(renderer='svg')
alt.LayerChart(…)
alt.Chart(source).mark_bar().encode(
x="month:T",
y="nonfarm_change:Q",
color=alt.condition(
alt.datum.nonfarm_change > 0,
alt.value("steelblue"), # The positive color
alt.value("orange") # The negative color
)
).properties(width=600).display(renderer='svg')
alt.Chart(…)
alt.Chart(source).mark_bar(
cornerRadiusTopLeft=3,
cornerRadiusTopRight=3
).encode(
x='month(date):O',
# y='PercentOfTotal:Q',
3
y='count():Q',
color='weather:N'
).properties(width=400).display(renderer='svg')
alt.Chart(…)
alt.Chart(source).transform_joinaggregate(
TotalTime='sum(Time)',
).transform_calculate(
PercentOfTotal="datum.Time / datum.TotalTime"
).mark_bar().encode(
alt.X('PercentOfTotal:Q', axis=alt.Axis(format='.0%')),
y='Activity:N'
).properties(width=400, height=200).display(renderer='svg')
alt.Chart(…)
alt.Chart(source).mark_bar().encode(
x='year:O',
y='sum(yield):Q',
color='year:N',
column='site:N',
# row='site:N'
).properties(height=200).display(renderer='svg')
alt.Chart(…)
alt.Chart(source).mark_bar().encode(
x='sum(yield)',
y='variety',
color='site'
).properties(height=200).display(renderer='svg')
alt.Chart(…)
4
1.2.8 Layered Bar Chart
alt.Chart(source).mark_bar(opacity=0.7).encode(
x='year(year):O',
y=alt.Y('net_generation:Q', stack=None),
color="source",
).properties(height=300, width=400).display(renderer='svg')
alt.Chart(…)
alt.Chart(source).mark_bar().encode(
x=alt.X('sum(yield)', stack="normalize"),
y='variety',
color='site'
).properties(height=250, width=400).display(renderer='svg')
alt.Chart(…)
alt.Chart(source).mark_bar().encode(
x='sum(yield):Q',
y=alt.Y('site:N', sort='-x')
).properties(height=250, width=400).display(renderer='svg')
alt.Chart(…)
bars = alt.Chart(source).mark_bar().encode(
x=alt.X('sum(yield):Q', stack='zero'),
y=alt.Y('variety:N'),
color=alt.Color('site')
)
5
text=alt.Text('sum(yield):Q', format='.1f')
)
alt.LayerChart(…)
alt.Chart(source).mark_area(
color="lightblue",
interpolate='step-after',
line=True
).encode(
x='date',
y='price'
).transform_filter(alt.datum.symbol == 'GOOG').display(renderer='svg')
alt.Chart(…)
line = alt.Chart(source).mark_line().encode(
x='Year',
y='mean(Miles_per_Gallon)'
)
band = alt.Chart(source).mark_errorband(extent='ci').encode(
x='Year',
y=alt.Y('Miles_per_Gallon', title='Miles/Gallon'),
)
(band + line).display(renderer='svg')
alt.LayerChart(…)
base = alt.Chart(source).properties(width=550)
line = base.mark_line().encode(
6
x='date',
y='price',
color='symbol'
)
rule = base.mark_rule().encode(
y='average(price)',
color='symbol',
size=alt.value(2)
)
(line + rule).display(renderer='svg')
alt.LayerChart(…)
alt.Chart(…)
alt.Chart(source).mark_line(point=True).encode(
x='x',
y='f(x)'
).display(renderer='svg')
alt.Chart(…)
7
1.3.6 Line Chart with Sequence Generator
alt.Chart(source).mark_line().transform_calculate(
sin='sin(datum.x)',
cos='cos(datum.x)'
).transform_fold(
['sin', 'cos']
).encode(
x='x:Q',
y='value:Q',
color='key:N'
).display(renderer='svg')
alt.Chart(…)
alt.Chart(source).mark_line().encode(
x='date',
y='price',
color='symbol',
strokeDash='symbol',
).display(renderer='svg')
alt.Chart(…)
8
color=alt.Gradient(
gradient='linear',
stops=[alt.GradientStop(color='white', offset=0),
alt.GradientStop(color='darkgreen', offset=1)],
x1=1, x2=1, y1=1,y2=0
)
).encode(
alt.X('date:T'),
alt.Y('price:Q')
).display(renderer='svg')
alt.Chart(…)
alt.Chart(…)
[28]: (alt.Chart(source).transform_fold(
['petalWidth',
'petalLength',
'sepalWidth',
'sepalLength'],
as_ = ['Measurement_type', 'value']
).transform_density(
density='value',
bandwidth=0.3,
9
groupby=['Measurement_type'],
extent= [0, 8]
).mark_area().encode(
alt.X('value:Q'),
alt.Y('density:Q'),
alt.Row('Measurement_type:N')
).properties(width=300, height=50)
).display(renderer='svg')
alt.Chart(…)
alt.Chart(…)
10
area1 = alt.Chart(source).mark_area(
clip=True,
interpolate='monotone'
).encode(
alt.X('x', scale=alt.Scale(zero=False, nice=False)),
alt.Y('y', scale=alt.Scale(domain=[0, 50]), title='y'),
opacity=alt.value(0.6)
).properties(
width=500,
height=75
)
area2 = area1.encode(
alt.Y('ny:Q', scale=alt.Scale(domain=[0, 50]))
).transform_calculate(
"ny", alt.datum.y - 50
)
(area1 + area2).display(renderer='svg')
alt.LayerChart(…)
lower = base.properties(
height=60
).add_selection(brush)
alt.VConcatChart(…)
11
1.4.6 Layered Area Chart
alt.Chart(source).mark_area(opacity=0.3).encode(
x="year:T",
y=alt.Y("net_generation:Q", stack=None),
color="source:N"
).properties(height=250).display(renderer='svg')
alt.Chart(…)
alt.Chart(source).mark_area().encode(
x="year:T",
y=alt.Y("net_generation:Q", stack="normalize"),
color="source:N"
).properties(height=250).display(renderer='svg')
alt.Chart(…)
1.4.8 Streamgraph
alt.Chart(source).mark_area().encode(
alt.X('yearmonth(date):T',
axis=alt.Axis(format='%Y', domain=False, tickSize=0)
),
alt.Y('sum(count):Q', stack='center', axis=None),
alt.Color('series:N',
scale=alt.Scale(scheme='category20b')
)
).display(renderer='svg')
alt.Chart(…)
12
row=alt.Row("source:N", sort=slist)
).properties(
height=100
).display(renderer='svg')
alt.Chart(…)
alt.Chart(source).mark_circle().encode(
alt.X('IMDB_Rating:Q', bin=True),
alt.Y('Rotten_Tomatoes_Rating:Q', bin=True),
size='count()'
).display(renderer='svg')
alt.Chart(…)
# Scatter Plot
points = alt.Chart(source).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color=alt.condition(brush, 'Cilinders:O', alt.value('grey'))
).add_selection(brush)
13
# Data Tables
horsepower = ranked_text.encode(text='Horsepower:N').
,→properties(title='Horsepower')
mpg = ranked_text.encode(text='Miles_per_Gallon:N').properties(title='MPG')
origin = ranked_text.encode(text='Origin:N').properties(title='Origin')
text = alt.hconcat(horsepower, mpg, origin) # Combine data tables
# Build chart
alt.hconcat(
points,
text
).resolve_legend(
color="independent"
).display(renderer='svg')
alt.HConcatChart(…)
x_ticks = base.mark_tick().encode(
alt.X('Miles_per_Gallon', axis=tick_axis),
alt.Y('Origin', title='', axis=tick_axis),
color=alt.condition(brush, 'Origin', alt.value('lightgrey'))
)
y_ticks = base.mark_tick().encode(
alt.X('Origin', title='', axis=tick_axis),
alt.Y('Horsepower', axis=tick_axis),
color=alt.condition(brush, 'Origin', alt.value('lightgrey'))
)
14
(y_ticks | (points & x_ticks)).display(renderer='svg')
alt.HConcatChart(…)
alt.Chart(source).mark_circle().encode(
alt.X('sepalLength', scale=alt.Scale(zero=False)),
alt.Y('sepalWidth', scale=alt.Scale(zero=False, padding=1)),
color='species',
size='petalWidth'
).display(renderer='svg')
alt.Chart(…)
base = alt.Chart(source).transform_quantile(
'u',
step=0.01,
as_ = ['p', 'v']
).transform_calculate(
uniform = 'quantileUniform(datum.p)',
normal = 'quantileNormal(datum.p)'
).mark_point().encode(
alt.Y('v:Q')
)
(base.encode(x='uniform:Q') | base.encode(x='normal:Q')).display(renderer='svg')
alt.HConcatChart(…)
alt.Chart(source).mark_circle().encode(
alt.X(alt.repeat("column"), type='quantitative'),
alt.Y(alt.repeat("row"), type='quantitative'),
color='Origin:N'
).properties(
width=150,
height=150
).repeat(
row=['Horsepower', 'Acceleration', 'Miles_per_Gallon'],
15
column=['Miles_per_Gallon', 'Acceleration', 'Horsepower']
).display(renderer='svg')
alt.RepeatChart(…)
alt.Chart(source).transform_calculate(
url='https://www.google.com/search?q=' + alt.datum.Name
).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color='Origin:N',
href='url:N',
tooltip=['Name:N'],
# tooltip=['Name:N', 'url:N']
).display(renderer='svg')
alt.Chart(…)
source = pd.DataFrame({
'x': np.arange(100),
'A': np.random.randn(100).cumsum(),
'B': np.random.randn(100).cumsum(),
'C': np.random.randn(100).cumsum(),
})
base = alt.Chart(source).mark_circle(opacity=0.5).transform_fold(
fold=['A', 'B', 'C'],
as_=['category', 'y']
).encode(
alt.X('x:Q'),
alt.Y('y:Q'),
alt.Color('category:N')
)
(base
+ base.transform_loess('x', 'y', groupby=['category']).mark_line(size=4)
).display(renderer='svg')
alt.LayerChart(…)
16
1.5.9 Scatter Plot with Rolling Mean
line = alt.Chart(source).mark_line(
color='red',
size=2
).transform_window(
rolling_mean='mean(temp_max)',
frame=[-30, 30]
).encode(
x='date:T',
y='rolling_mean:Q'
)
(points + line).display(renderer='svg')
alt.LayerChart(…)
1.5.10 Stripplot
[45]: source = data.movies.url
17
).transform_calculate(
# Generate Gaussian jitter with a Box-Muller transform
jitter='sqrt(-2*log(random()))*cos(2*PI*random())'
).configure_facet(
spacing=0
).configure_view(
stroke=None
)
(stripplot).display(renderer='svg')
alt.Chart(…)
1.6 Histrograms
1.6.1 Histogram with Responsive Bins
brush = alt.selection_interval(encodings=['x'])
base = alt.Chart(source).transform_calculate(
time="hours(datum.date) + minutes(datum.date) / 60"
).mark_bar().encode(
y='count():Q'
).properties(
width=600,
height=100
)
alt.vconcat(
base.encode(
alt.X('time:Q',
bin=alt.Bin(maxbins=30, extent=brush),
scale=alt.Scale(domain=brush)
)
),
base.encode(
alt.X('time:Q', bin=alt.Bin(maxbins=30)),
).add_selection(brush)
).display(renderer='svg')
alt.VConcatChart(…)
18
1.6.2 Layered Histogram
alt.Chart(source).transform_fold(
['Trial A', 'Trial B', 'Trial C'],
as_=['Experiment', 'Measurement']
).mark_area(
opacity=0.3,
interpolate='step'
).encode(
alt.X('Measurement:Q', bin=alt.Bin(maxbins=100)),
alt.Y('count()', stack=None),
alt.Color('Experiment:N')
).display(renderer='svg')
alt.Chart(…)
1.7 Maps
1.7.1 Choropleth Map
alt.Chart(counties).mark_geoshape().encode(
color='rate:Q'
).transform_lookup(
lookup='id',
from_=alt.LookupData(source, 'id', ['rate'])
).project(
type='albersUsa'
).properties(
width=500,
height=300
).display(renderer='svg')
alt.Chart(…)
19
1.7.2 World Map
alt.LayerChart(…)
base = alt.Chart(source).mark_geoshape(
fill='#666666',
stroke='white'
).properties(
width=300,
height=180
)
alt.concat(*charts, columns=2)
[50]: alt.ConcatChart(…)
from https://github.com/altair-viz/altair/issues/588
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = world[world.continent!='Antarctica'] # do not display Antarctica
20
df = alt.InlineData(values = world.to_json(), #geopandas to geojson string
# root object type is "FeatureCollection" but we need␣
,→its features
format = alt.DataFormat(property='features',type='json'))
alt.Chart(df).mark_geoshape(
).encode(
color='properties.pop_est:Q', # DataFrame fields are accessible through a␣
,→"properties" object
tooltip='properties.name:N'
).properties(
projection={"type":'mercator'},
width=500,
height=300
).properties(width=600, height=400).display(renderer='svg')
alt.Chart(…)
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
df = world[world.continent == continent]
brush = alt.selection_single(encodings=["y"], on="mouseover", empty='none')
color = alt.Color('pop_est', scale= alt.Scale(type='pow', exponent=0.4))
bars = (alt.Chart(df).mark_bar().encode(
x=alt.X('pop_est', scale=alt.Scale(nice=False)),
y=alt.Y('name', sort=alt.EncodingSortField(field='pop_est', op='sum',␣
,→order='descending')),
tooltip=['name','pop_est','gdp_md_est'],
color=alt.condition(brush, alt.value('red'), color)
).add_selection(
brush
).properties(
width=200,
height=450
)
)
countries = (alt.Chart(df).mark_geoshape().project().encode(
color=alt.condition(
brush,
alt.value('red'),
color,
),
tooltip=['name','pop_est','gdp_md_est'],
).properties(
21
width=300,
height=450,
title=f'{continent} population'
)
)
(bars | countries).display(renderer='svg')
alt.HConcatChart(…)
x='date:T',
y='temp:Q'
).transform_sample(7000)
points.display(renderer='svg')
alt.Chart(…)
bands = alt.Chart(temps).mark_area(opacity=0.7).encode(
22
alt.X('yearmonthdate(date):T'),
alt.Y('ci0(temp)', scale=alt.Scale(domain=[30,80])),
alt.Y2('ci1(temp)')
)
(bands + mean + max+ points).properties(width=700, height=400).
,→configure_view(fill='lightgray', opacity=0.4).display(renderer='svg')
alt.LayerChart(…)
alt.LayerChart(…)
alt.Chart(…)
[58]: alt.Chart(temps).mark_area().encode(
x='month(date):T',
y=alt.Y('mean(temp):Q', title = 'Temperatura media °F', scale=alt.
,→Scale(domain=[20, 70]))
).properties(width=400).display(renderer='svg')
alt.Chart(…)
23
).display(renderer='svg')
alt.Chart(…)
)
texts = alt.Chart(temps).mark_text(align='center', size=9, color='black').
,→encode(
alt.X('month(date):O', title='mes'),
alt.Y('hora:O',
scale=alt.Scale(domain=list(range(6,24)) + list(range(0,7)))),
alt.Text('mean(temp_C):Q', format=",.1f"),
# color=alt.condition('datum.temp_C < 10', alt.value('black'), alt.
,→value('red'))
)
(rect + texts).properties(width=600, height=600).display(renderer='svg')
alt.LayerChart(…)
rect = alt.Chart(data.movies.url).mark_rect().encode(
alt.X('IMDB_Rating:Q', bin=True),
alt.Y('Rotten_Tomatoes_Rating:Q', bin=True),
alt.Color('count()',
scale=alt.Scale(scheme='greenblue'),
legend=alt.Legend(title='Total Records')
)
)
circ = rect.mark_point().encode(
alt.ColorValue('grey'),
24
alt.Size('count()',
legend=alt.Legend(title='Records in Selection')
)
).transform_filter(
pts
)
bar = alt.Chart(source).mark_bar().encode(
x = alt.X('Major_Genre:N', sort='y'),
y='count()',
color=alt.condition(pts, alt.ColorValue("steelblue"), alt.
,→ColorValue("grey"))
).properties(
width=550,
height=200
).add_selection(pts)
alt.vconcat(
rect + circ,
bar
).resolve_legend(
color="independent",
size="independent"
).display(renderer='svg')
alt.VConcatChart(…)
points = alt.Chart(source).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon',
color=alt.condition(brush, 'Origin', alt.value('lightgray'))
).add_selection(brush)
bars = alt.Chart(source).mark_bar().encode(
y='Origin',
color='Origin',
x='count()'
).transform_filter(brush)
alt.VConcatChart(…)
25
1.10.3 Multi-Line Highlight (single selection)
base = alt.Chart(source).encode(
x='date:T',
y='price:Q',
color='symbol:N'
)
points = base.mark_circle().encode(
opacity=alt.value(0)
).add_selection(
highlight
).properties(
width=600
)
lines = base.mark_line().encode(
size=alt.condition(~highlight, alt.value(1), alt.value(3))
)
(points + lines).display(renderer='svg')
alt.LayerChart(…)
26
# x='count()'
).add_selection(brush)
alt.HConcatChart(…)
alt.Chart(…)
27
1.11.2 Gantt Chart
[66]: source = pd.DataFrame([
{"task": "A", "start": 1, "end": 3},
{"task": "B", "start": 3, "end": 8},
{"task": "C", "start": 8, "end": 10}
])
alt.Chart(source).mark_bar().encode(
x='start',
x2='end',
y='task'
).display(renderer='svg')
alt.Chart(…)
base = alt.Chart(source).encode(
alt.X('month(date):T', axis=alt.Axis(title=None))
)
alt.Y('average(temp_max)',
axis=alt.Axis(title='Avg. Temperature (°C)', titleColor='#57A44C')),
alt.Y2('average(temp_min)')
)
alt.Y('average(precipitation)',
axis=alt.Axis(title='Precipitation (inches)', titleColor='#5276A7'))
)
(area + line).resolve_scale(
y = 'independent'
).display(renderer='svg')
alt.LayerChart(…)
step = 20
28
overlap = 1
alt.Chart(source, height=step).transform_timeunit(
Month='month(date)'
).transform_joinaggregate(
mean_temp='mean(temp_max)', groupby=['Month']
).transform_bin(
['bin_max', 'bin_min'], 'temp_max'
).transform_aggregate(
value='count()', groupby=['Month', 'mean_temp', 'bin_min', 'bin_max']
).transform_impute(
impute='value', groupby=['Month', 'mean_temp'], key='bin_min', value=0
).mark_area(
interpolate='monotone',
fillOpacity=0.8,
stroke='lightgray',
strokeWidth=0.5
).encode(
alt.X('bin_min:Q', bin='binned', title='Maximum Daily Temperature (C)'),
alt.Y(
'value:Q',
scale=alt.Scale(range=[step, -step * overlap]),
axis=None
),
alt.Fill(
'mean_temp:Q',
legend=None,
scale=alt.Scale(domain=[30, 5], scheme='redyellowblue')
)
).facet(
row=alt.Row(
'Month:T',
title=None,
header=alt.Header(labelAngle=0, labelAlign='right', format='%B')
)
).properties(
title='Seattle Weather',
bounds='flush'
).configure_facet(
spacing=0
).configure_view(
stroke=None
).configure_title(
anchor='end'
).display(renderer='svg')
alt.FacetChart(…)
29
1.11.5 Boxplot
alt.Chart(source).mark_boxplot().encode(
x='age:O',
y='people:Q'
).properties(width=500).display(renderer='svg')
alt.Chart(…)
30