1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
"""
Interactive Scatter Plot and Linked Layered Histogram
=====================================================
This example shows how to link a scatter plot and a histogram
together such that clicking on a point in the scatter plot will
isolate the distribution corresponding to that point, and vice versa.
"""
# category: interactive charts
import altair as alt
import pandas as pd
import numpy as np
# generate fake data
source = pd.DataFrame({
'gender': ['M']*1000 + ['F']*1000,
'height':np.concatenate((
np.random.normal(69, 7, 1000), np.random.normal(64, 6, 1000)
)),
'weight': np.concatenate((
np.random.normal(195.8, 144, 1000), np.random.normal(167, 100, 1000)
)),
'age': np.concatenate((
np.random.normal(45, 8, 1000), np.random.normal(51, 6, 1000)
))
})
selector = alt.selection_point(fields=['gender'])
color_scale = alt.Scale(domain=['M', 'F'],
range=['#1FC3AA', '#8624F5'])
base = alt.Chart(source).properties(
width=250,
height=250
).add_params(selector)
points = base.mark_point(filled=True, size=200).encode(
x=alt.X('mean(height):Q', scale=alt.Scale(domain=[0,84])),
y=alt.Y('mean(weight):Q', scale=alt.Scale(domain=[0,250])),
color=alt.condition(
selector,
'gender:N',
alt.value('lightgray'),
scale=color_scale),
)
hists = base.mark_bar(opacity=0.5, thickness=100).encode(
x=alt.X('age',
bin=alt.Bin(step=5), # step keeps bin size the same
scale=alt.Scale(domain=[0,100])),
y=alt.Y('count()',
stack=None,
scale=alt.Scale(domain=[0,350])),
color=alt.Color('gender:N',
scale=color_scale)
).transform_filter(
selector
)
points | hists
|