-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaltairflow.py
71 lines (59 loc) · 2 KB
/
altairflow.py
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
64
65
66
67
68
69
70
71
import math, random, os
from metaflow import step, FlowSpec, card
class AltairFlow(FlowSpec):
@card(type='html')
@step
def start(self):
import altair as alt
from vega_datasets import data
source = data.seattle_weather()
scale = alt.Scale(domain=['sun', 'fog', 'drizzle', 'rain', 'snow'],
range=['#e7ba52', '#a7a7a7', '#aec7e8', '#1f77b4', '#9467bd'])
color = alt.Color('weather:N', scale=scale)
# We create two selections:
# - a brush that is active on the top panel
# - a multi-click that is active on the bottom panel
brush = alt.selection_interval(encodings=['x'])
click = alt.selection_multi(encodings=['color'])
# Top panel is scatter plot of temperature vs time
points = alt.Chart().mark_point().encode(
alt.X('monthdate(date):T', title='Date'),
alt.Y('temp_max:Q',
title='Maximum Daily Temperature (C)',
scale=alt.Scale(domain=[-5, 40])
),
color=alt.condition(brush, color, alt.value('lightgray')),
size=alt.Size('precipitation:Q', scale=alt.Scale(range=[5, 200]))
).properties(
width=550,
height=300
).add_selection(
brush
).transform_filter(
click
)
# Bottom panel is a bar chart of weather type
bars = alt.Chart().mark_bar().encode(
x='count()',
y='weather:N',
color=alt.condition(click, color, alt.value('lightgray')),
).transform_filter(
brush
).properties(
width=550,
).add_selection(
click
)
chart = alt.vconcat(
points,
bars,
data=source,
title="Seattle Weather: 2012-2015"
)
self.html = chart.to_html()
self.next(self.end)
@step
def end(self):
pass
if __name__ == '__main__':
AltairFlow()