Skip to content

Instantly share code, notes, and snippets.

@blaulan
Created April 23, 2024 06:14
Show Gist options
  • Save blaulan/946d761535d64d5d445405896540a483 to your computer and use it in GitHub Desktop.
Save blaulan/946d761535d64d5d445405896540a483 to your computer and use it in GitHub Desktop.
plot pie chart in folium
import folium.plugins
def plot_pie_chart(layer, coord, radius, numbers, labels):
total = sum(numbers)
start_angle = 0
for index, number in enumerate(numbers):
if number == 0:
continue
percent = number / total
angle = round(percent * 360, 1)
color = colors[index % 30]
folium.plugins.SemiCircle(
coord,
radius=radius,
start_angle=start_angle,
stop_angle=min(start_angle+angle, 360),
weight=1,
color=color,
opacity=0.5,
fill_color=color,
fill_opacity=0.5,
tooltip='{} {:.0%}'.format(labels[index], percent),
).add_to(layer)
start_angle += angle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment