Created
April 23, 2024 06:14
-
-
Save blaulan/946d761535d64d5d445405896540a483 to your computer and use it in GitHub Desktop.
plot pie chart in folium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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