import pandas as pd
import altair as alt
from vega_datasets import data
df = data.iris()
df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 150 entries, 0 to 149 Data columns (total 5 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 sepalLength 150 non-null float64 1 sepalWidth 150 non-null float64 2 petalLength 150 non-null float64 3 petalWidth 150 non-null float64 4 species 150 non-null object dtypes: float64(4), object(1) memory usage: 6.0+ KB
To make selections in Altair, we need three things:
There are two types of selection available; a selection interval, which is called a brush in some other plotting packages, and clickable selections. We'll start with the selection interval.
Where the selection interval really comes into its own is in related subcharts, as the selection is repeated across the charts. An example makes that clearer.
selector = alt.selection_interval()
condition = alt.condition(selector,
'species',
alt.value('lightgray'))
petals = alt.Chart(df).mark_circle().encode(
x='petalLength',
y='petalWidth',
color=condition,
tooltip=df.columns.to_list())
sepals = alt.Chart(df).mark_circle().encode(
x='sepalLength',
y='sepalWidth',
color=condition,
tooltip=df.columns.to_list())
(petals | sepals).add_selection(selector)
A single selection is just as you'd expect - point and click.
selector = alt.selection_single()
condition = alt.condition(selector,
'species',
alt.value('lightgray'))
petals = alt.Chart(df).mark_circle().encode(
x='petalLength',
y='petalWidth',
color=condition,
tooltip=df.columns.to_list())
sepals = alt.Chart(df).mark_circle().encode(
x='sepalLength',
y='sepalWidth',
color=condition,
tooltip=df.columns.to_list())
(petals | sepals).add_selection(selector)
But because pointing and clicking can be tiresome, there's an on parameter we can set to mouseover.
selector = alt.selection_single(on="mouseover")
condition = alt.condition(selector,
'species',
alt.value('lightgray'))
petals = alt.Chart(df).mark_circle().encode(
x='petalLength',
y='petalWidth',
color=condition,
tooltip=df.columns.to_list())
sepals = alt.Chart(df).mark_circle().encode(
x='sepalLength',
y='sepalWidth',
color=condition,
tooltip=df.columns.to_list())
(petals | sepals).add_selection(selector)
To make multiple selections, we change our selector to .selection_multi()
, set on="mouseover"
, and set toggle=False
.
selector = alt.selection_multi(on="mouseover", toggle=False)
condition = alt.condition(selector,
'species',
alt.value('lightgray'))
petals = alt.Chart(df).mark_circle().encode(
x='petalLength',
y='petalWidth',
color=condition,
tooltip=df.columns.to_list())
sepals = alt.Chart(df).mark_circle().encode(
x='sepalLength',
y='sepalWidth',
color=condition,
tooltip=df.columns.to_list())
(petals | sepals).add_selection(selector)
We can use the fields
parameter to select by category.
selector = alt.selection_multi(fields=['species'])
condition = alt.condition(selector,
'species',
alt.value('lightgray'))
petals = alt.Chart(df).mark_circle().encode(
x='petalLength',
y='petalWidth',
color=condition,
tooltip=df.columns.to_list())
sepals = alt.Chart(df).mark_circle().encode(
x='sepalLength',
y='sepalWidth',
color=condition,
tooltip=df.columns.to_list())
(petals | sepals).add_selection(selector)
Selecting by legend may be the most useful method of all. Again, the code is very similar to what it's been all along, but there are two points to note.
alt.Color()
value, rather than just the column name. This means we can turn off the default legend.legend
object, to join our selector
, condition
, and chart
objects.selector = alt.selection_multi(fields=['species'])
condition = alt.condition(selector,
alt.Color('species:N', legend=None),
alt.value('lightgray'))
chart = alt.Chart(df).mark_circle().encode(
x='petalLength',
y='petalWidth',
color=condition,
tooltip=df.columns.to_list())
legend = alt.Chart(df).mark_circle().encode(
y=alt.Y('species:N', axis=alt.Axis(orient='right')),
color=condition)
(chart | legend).add_selection(selector)