Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mebaysan/ae9164820ffc837950d445bd5f24497b to your computer and use it in GitHub Desktop.
Save mebaysan/ae9164820ffc837950d445bd5f24497b to your computer and use it in GitHub Desktop.
We can add dots on a choropleth map
import json
import requests
import plotly.express as px
import plotly.graph_objects as go
geojson = requests.get(
"https://gist.githubusercontent.com/mebaysan/9be56dd1ca5659c0ff7ea5e2b5cf6479/raw/6d7a77d8a2892bd59f401eb87bd82d7f48642a58/turkey-geojson.json"
).json()
geoDict = {}
for i in geojson["features"]:
geoDict[i["properties"]["name"]] = i["id"]
iller_df = pd.read_csv(
"https://gist.githubusercontent.com/mebaysan/7a4ba8531187fa8703ff1f22692d5fa6/raw/df4e85262ba2a4f6d6045f06f417b853fb67e78c/il.csv"
)
iller_df = iller_df[iller_df["il_adi"] == "İSTANBUL"]
iller_df = iller_df.replace("İSTANBUL", "İstanbul")
iller_df["GeoID"] = iller_df["il_adi"].apply(lambda x: geoDict[x])
fig = px.choropleth_mapbox(
iller_df,
geojson=geojson,
locations="GeoID",
center={"lat": iller_df["lat"].values[0], "lon": iller_df["lon"].values[0]},
mapbox_style="carto-positron",
zoom=8,
opacity=0.5,
color_discrete_sequence=["#01317d"],
)
worksites = """
[{
"ilce_id": 452,
"il_plaka": 34,
"ilce_adi": "ÜMRANİYE",
"lat": 41.0303,
"lon": 29.1065,
"northeast_lat": 41.065545,
"northeast_lon": 29.19719,
"southwest_lat": 40.988965,
"southwest_lon": 29.080313
},
{
"ilce_id": 453,
"il_plaka": 34,
"ilce_adi": "ÜSKÜDAR",
"lat": 41.032236,
"lon": 29.031938,
"northeast_lat": 41.077997,
"northeast_lon": 29.091718,
"southwest_lat": 40.9925,
"southwest_lon": 29.006362
}]
"""
worksites_json = json.loads(worksites)
for worksite in worksites_json:
fig.add_trace(
go.Scattermapbox(
lon=[worksite["lon"]],
lat=[worksite["lat"]],
text=worksite["ilce_adi"],
mode="markers",
marker_color="#8EC9FF",
marker_size=10,
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment