-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_functions.py
80 lines (69 loc) · 2.26 KB
/
map_functions.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
72
73
74
75
76
77
78
79
80
import pandas as pd
import altair as alt
import numpy as np
from vega_datasets import data
pd.options.mode.chained_assignment = None
def convert_ids(df):
df = df[df["State ANSI"].notnull()]
df["State ANSI"] = df["State ANSI"].astype(int)
df["County ANSI"] = df["County ANSI"].astype(int)
df["id"] = df["State ANSI"] * 1000 + df["County ANSI"]
return df
def plot_counties(df, variable, color_scheme="blueorange", domain=None, show_pdf=True):
"""Plot a map of variable 'variable' in dataframe 'df'"""
counties = alt.topo_feature(data.us_10m.url, "counties")
df.replace({0: np.nan}, inplace=True)
df = convert_ids(df)
if domain:
scale = alt.Scale(scheme=color_scheme, domain=domain, clamp=True)
else:
scale = alt.Scale(scheme=color_scheme)
map_chart = (
alt.Chart(counties)
.mark_geoshape()
.encode(
color=alt.Color(
variable + ":Q",
scale=scale,
),
tooltip=["id:O", variable + ":Q"],
)
.transform_lookup(lookup="id", from_=alt.LookupData(df, "id", [variable]))
.project(type="albersUsa")
.properties(width=800, height=400)
)
pdf_chart = (
alt.Chart(df)
.mark_bar()
.encode(
y="count()",
x=alt.X(variable + ":Q", bin=alt.Bin(maxbins=40)),
)
.transform_lookup(lookup="id", from_=alt.LookupData(df, "id", [variable]))
)
if show_pdf:
return map_chart | pdf_chart
else:
return map_chart
def plot_states(df, variable, color_scheme="blueorange", domain=None):
id_col = "State Code"
states = alt.topo_feature(data.us_10m.url, "states")
if domain:
scale = alt.Scale(scheme=color_scheme, domain=domain, clamp=True)
else:
scale = alt.Scale(scheme=color_scheme)
chart = (
alt.Chart(states)
.mark_geoshape()
.encode(
color=alt.Color(
variable + ":Q",
scale=scale,
),
tooltip=["id:O", variable + ":Q"],
)
.transform_lookup(lookup="id", from_=alt.LookupData(df, id_col, [variable]))
.project(type="albersUsa")
.properties(width=800, height=400)
)
return chart