-
Notifications
You must be signed in to change notification settings - Fork 766
/
co2_concentration.py
64 lines (57 loc) · 1.61 KB
/
co2_concentration.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
"""
Atmospheric CO2 Concentration
-----------------------------
This example is a fully developed line chart that uses a window transformation.
It was inspired by `Gregor Aisch's work at datawrapper
<https://www.datawrapper.de/_/OHgEm/>`_.
"""
# category: case studies
import altair as alt
from vega_datasets import data
source = data.co2_concentration.url
base = alt.Chart(
source,
title="Carbon Dioxide in the Atmosphere"
).transform_calculate(
year="year(datum.Date)"
).transform_calculate(
decade="floor(datum.year / 10)"
).transform_calculate(
scaled_date="(datum.year % 10) + (month(datum.Date)/12)"
).transform_window(
first_date='first_value(scaled_date)',
last_date='last_value(scaled_date)',
sort=[{"field": "scaled_date", "order": "ascending"}],
groupby=['decade'],
frame=[None, None]
).transform_calculate(
end=(
"datum.first_date === datum.scaled_date ? 'first'"
": datum.last_date === datum.scaled_date ? 'last'"
": null"
)
).encode(
alt.X("scaled_date:Q")
.title("Year into Decade")
.axis(tickCount=11),
alt.Y("CO2:Q")
.title("CO2 concentration in ppm")
.scale(zero=False)
)
line = base.mark_line().encode(
alt.Color("decade:O")
.scale(scheme="magma")
.legend(None)
)
text = base.encode(text="year:N")
start_year = text.transform_filter(
alt.datum.end == 'first'
).mark_text(baseline="top")
end_year = text.transform_filter(
alt.datum.end == 'last'
).mark_text(baseline="bottom")
(line + start_year + end_year).configure_text(
align="left",
dx=1,
dy=3
).properties(width=600, height=375)