add color ramp elections pirates
This commit is contained in:
parent
82fa1ead75
commit
a675c633a1
File diff suppressed because one or more lines are too long
|
@ -1,5 +1,12 @@
|
|||
import json
|
||||
import folium
|
||||
from sklearn.preprocessing import MinMaxScaler
|
||||
import random
|
||||
from branca.colormap import linear
|
||||
|
||||
def get_random_color(colors):
|
||||
return random.choice(colors)
|
||||
|
||||
|
||||
# Charger les données depuis le fichier JSON
|
||||
with open("elections_2024.json", "r") as f:
|
||||
|
@ -13,36 +20,55 @@ with open("departements.geojson", "r") as f:
|
|||
m = folium.Map(location=[46.2276, 2.2137], zoom_start=6)
|
||||
|
||||
# Définir les couleurs pour le gradient
|
||||
colors = ['#f2f0f7', '#dadaeb', '#bcbddc', '#9e9ac8', '#807dba', '#6a51a3', '#54278f', '#3f007d']
|
||||
colors = ['#ffffff', '#b9b9b9', '#777777', '#353535']
|
||||
|
||||
# Normaliser les valeurs de percent_exprimes
|
||||
percent_exprimes_values = [[float(value["pourcentage_exprimes"].replace(',', '.'))] for departement, values in data.items() for value in values if value["parti"] == "PARTI PIRATE"]
|
||||
scaler = MinMaxScaler()
|
||||
scaler.fit(percent_exprimes_values)
|
||||
|
||||
# Calculer la valeur maximale de pourcentage_exprimes
|
||||
max_percent_exprimes = max(float(value["pourcentage_exprimes"].replace(',', '.')) for departement, values in data.items() for value in values if value["parti"] == "PARTI PIRATE")
|
||||
|
||||
|
||||
colormap = linear.YlGn_09.scale(
|
||||
0, max_percent_exprimes
|
||||
)
|
||||
|
||||
# Parcourir les départements et ajouter des polygones avec le gradient de couleur
|
||||
for departement, values in data.items():
|
||||
for value in values:
|
||||
if value["parti"] == "PARTI PIRATE":
|
||||
percent_exprimes = float(value["pourcentage_exprimes"].replace(',', '.'))
|
||||
color_index = int((percent_exprimes / max_percent_exprimes) * (len(colors) - 1))
|
||||
print(color_index)
|
||||
normalized_percent_exprimes = scaler.transform([[percent_exprimes]])[0][0]
|
||||
|
||||
print(normalized_percent_exprimes)
|
||||
|
||||
color_index = int(normalized_percent_exprimes * (len(colors) - 1))
|
||||
color = colors[color_index]
|
||||
print('département:', departement, 'couleur:', color_index, color)
|
||||
|
||||
for feature in departements_geo["features"]:
|
||||
|
||||
if feature["properties"]["code"] == departement:
|
||||
# Ajouter le champ percent_exprimes à l'élément GeoJSON
|
||||
feature["properties"]["percent_exprimes"] = percent_exprimes
|
||||
feature["properties"]["bg_fill"] = color
|
||||
|
||||
folium.GeoJson(
|
||||
feature,
|
||||
name=f"Département {departement}",
|
||||
style_function=lambda x: {
|
||||
"fillColor": color,
|
||||
style_function=lambda feature: {
|
||||
# "fillColor": get_random_color(colors),
|
||||
"fillColor": colormap(feature["properties"]["percent_exprimes"]),
|
||||
# "fillColor": '#9e9ac8',
|
||||
"color": "black",
|
||||
"weight": 1,
|
||||
"fillOpacity": 0.7
|
||||
},
|
||||
tooltip=folium.features.GeoJsonTooltip(
|
||||
fields=['nom', 'percent_exprimes'],
|
||||
aliases=['Département:', 'Pourcentage:'],
|
||||
style=f"background-color: {color};font-size: 1.5rem;",
|
||||
fields=['nom', 'percent_exprimes', 'bg_fill'],
|
||||
aliases=['Département:', 'Pourcentage:','Rempliassage'],
|
||||
# style="background-color: white;",
|
||||
localize=True,
|
||||
sticky=False,
|
||||
labels=True,
|
||||
|
@ -54,7 +80,7 @@ for departement, values in data.items():
|
|||
popup=folium.features.GeoJsonPopup(
|
||||
fields=['percent_exprimes'],
|
||||
aliases=['Pourcentage:'],
|
||||
style="background-color: white; font-size: 1.5rem;",
|
||||
# style="background-color: white;",
|
||||
localize=True,
|
||||
sticky=False,
|
||||
labels=True,
|
||||
|
|
Loading…
Reference in New Issue