-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathissue_1989.py
More file actions
101 lines (84 loc) · 2.61 KB
/
Copy pathissue_1989.py
File metadata and controls
101 lines (84 loc) · 2.61 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import io
import branca
import geopandas
import pandas as pd
import requests
import streamlit as st
from streamlit_folium import st_folium
import folium
st.set_page_config(layout="wide")
response = requests.get(
"https://gist.githubusercontent.com/tvpmb/4734703/raw/"
"b54d03154c339ed3047c66fefcece4727dfc931a/US%2520State%2520List"
)
abbrs = pd.read_json(io.StringIO(response.text))
income = pd.read_csv(
"https://raw.githubusercontent.com/pri-data/50-states/master/data/income-counties-states-national.csv",
dtype={"fips": str},
)
income["income-2015"] = pd.to_numeric(income["income-2015"], errors="coerce")
data = requests.get(
"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_states.json"
).json()
states = geopandas.GeoDataFrame.from_features(data, crs="EPSG:4326")
statesmerge = states.merge(abbrs, how="left", left_on="name", right_on="name")
statesmerge["geometry"] = statesmerge.geometry.simplify(0.05)
statesmerge["medianincome"] = statesmerge.merge(
income.groupby("state")["income-2015"].median(),
how="left",
left_on="alpha-2",
right_on="state",
)["income-2015"]
statesmerge["change"] = statesmerge.merge(
income.groupby("state")["change"].median(),
how="left",
left_on="alpha-2",
right_on="state",
)["change"]
statesmerge["empty"] = None
colormap = branca.colormap.LinearColormap(
vmin=statesmerge["change"].quantile(0.0),
vmax=statesmerge["change"].quantile(1),
colors=["red", "orange", "lightblue", "green", "darkgreen"],
caption="State Level Median County Household Income (%)",
)
m = folium.Map(location=[35.3, -97.6], zoom_start=4)
popup = folium.GeoJsonPopup(
fields=["name", "change"],
aliases=["State", "% Change"],
localize=True,
labels=True,
style="background-color: yellow;",
)
tooltip = folium.GeoJsonTooltip(
fields=["name", "medianincome", "change", "empty"],
aliases=["State:", "2015 Median Income(USD):", "Median % Change:", "empty"],
localize=True,
sticky=False,
labels=True,
style="""
background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
max_width=800,
)
g = folium.GeoJson(
statesmerge,
style_function=lambda x: {
"fillColor": (
colormap(x["properties"]["change"])
if x["properties"]["change"] is not None
else "transparent"
),
"color": "black",
"fillOpacity": 0.4,
},
tooltip=tooltip,
popup=popup,
).add_to(m)
colormap.add_to(m)
html = m.get_root().render()
st_folium(m, width=700, height=500)
st.code(html)