Skip to content

Instantly share code, notes, and snippets.

@sjtalkar
Created December 16, 2020 02:28
Show Gist options
  • Save sjtalkar/1445f6b3eafe868aa4376bf4218caff0 to your computer and use it in GitHub Desktop.
Save sjtalkar/1445f6b3eafe868aa4376bf4218caff0 to your computer and use it in GitHub Desktop.
Creating a Folium map
def createRentalMap(df, fixed_radius, nbd_or_grp):
# Initialize a Folium map. Center it to the mean of latitude and longitude of the entire dataset
this_map = folium.Map(
location=[df["latitude"].mean(), df["longitude"].mean()],
tiles="CartoDB positron",
zoom_start=13,
)
def plotDot(point_row, fixed_radius, nbd_or_grp):
if fixed_radius:
radius = 30
else:
radius = point_row["nbd_count_normalized"]
if nbd_or_grp == "neighbourhood":
text = f'{point_row["neighbourhood_cleansed"]}'
else:
text = f'{point_row["neighbourhood_group_cleansed"]}'
folium.CircleMarker(
location=[point_row["latitude"], point_row["longitude"]],
radius=radius,
weight=3,
color=point_row["glow_marker_color"],
fill_color=point_row["glow_marker_color"],
fill_opacity=0.7,
).add_child(folium.Tooltip(f"{point_row.name}: {text}")).add_to(this_map)
# Add text of count within circle
folium.map.Marker(
[point_row["latitude"], point_row["longitude"]],
icon=DivIcon(
icon_size=(150, 36),
icon_anchor=(0, 0),
html='<div style="font-size: 9pt font-weight:bold">%s</div>' % text,
),
).add_to(this_map)
return
# Create circle markers for every listing by applying the plotDot function
if nbd_or_grp == "neighbourhood":
df[
[
"latitude",
"longitude",
"glow_marker_color",
"nbd_count_normalized",
"neighbourhood_cleansed",
]
].apply(plotDot, args=(fixed_radius, nbd_or_grp), axis=1)
else:
df[
[
"latitude",
"longitude",
"glow_marker_color",
"nbd_count_normalized",
"neighbourhood_group_cleansed",
]
].apply(plotDot, args=(fixed_radius, nbd_or_grp), axis=1)
return this_map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment