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 json
|
||||||
import folium
|
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
|
# Charger les données depuis le fichier JSON
|
||||||
with open("elections_2024.json", "r") as f:
|
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)
|
m = folium.Map(location=[46.2276, 2.2137], zoom_start=6)
|
||||||
|
|
||||||
# Définir les couleurs pour le gradient
|
# 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")
|
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
|
# Parcourir les départements et ajouter des polygones avec le gradient de couleur
|
||||||
for departement, values in data.items():
|
for departement, values in data.items():
|
||||||
for value in values:
|
for value in values:
|
||||||
if value["parti"] == "PARTI PIRATE":
|
if value["parti"] == "PARTI PIRATE":
|
||||||
percent_exprimes = float(value["pourcentage_exprimes"].replace(',', '.'))
|
percent_exprimes = float(value["pourcentage_exprimes"].replace(',', '.'))
|
||||||
color_index = int((percent_exprimes / max_percent_exprimes) * (len(colors) - 1))
|
normalized_percent_exprimes = scaler.transform([[percent_exprimes]])[0][0]
|
||||||
print(color_index)
|
|
||||||
|
print(normalized_percent_exprimes)
|
||||||
|
|
||||||
|
color_index = int(normalized_percent_exprimes * (len(colors) - 1))
|
||||||
color = colors[color_index]
|
color = colors[color_index]
|
||||||
|
print('département:', departement, 'couleur:', color_index, color)
|
||||||
|
|
||||||
for feature in departements_geo["features"]:
|
for feature in departements_geo["features"]:
|
||||||
|
|
||||||
if feature["properties"]["code"] == departement:
|
if feature["properties"]["code"] == departement:
|
||||||
# Ajouter le champ percent_exprimes à l'élément GeoJSON
|
# Ajouter le champ percent_exprimes à l'élément GeoJSON
|
||||||
feature["properties"]["percent_exprimes"] = percent_exprimes
|
feature["properties"]["percent_exprimes"] = percent_exprimes
|
||||||
|
feature["properties"]["bg_fill"] = color
|
||||||
|
|
||||||
folium.GeoJson(
|
folium.GeoJson(
|
||||||
feature,
|
feature,
|
||||||
name=f"Département {departement}",
|
name=f"Département {departement}",
|
||||||
style_function=lambda x: {
|
style_function=lambda feature: {
|
||||||
"fillColor": color,
|
# "fillColor": get_random_color(colors),
|
||||||
|
"fillColor": colormap(feature["properties"]["percent_exprimes"]),
|
||||||
|
# "fillColor": '#9e9ac8',
|
||||||
"color": "black",
|
"color": "black",
|
||||||
"weight": 1,
|
"weight": 1,
|
||||||
"fillOpacity": 0.7
|
"fillOpacity": 0.7
|
||||||
},
|
},
|
||||||
tooltip=folium.features.GeoJsonTooltip(
|
tooltip=folium.features.GeoJsonTooltip(
|
||||||
fields=['nom', 'percent_exprimes'],
|
fields=['nom', 'percent_exprimes', 'bg_fill'],
|
||||||
aliases=['Département:', 'Pourcentage:'],
|
aliases=['Département:', 'Pourcentage:','Rempliassage'],
|
||||||
style=f"background-color: {color};font-size: 1.5rem;",
|
# style="background-color: white;",
|
||||||
localize=True,
|
localize=True,
|
||||||
sticky=False,
|
sticky=False,
|
||||||
labels=True,
|
labels=True,
|
||||||
|
@ -54,7 +80,7 @@ for departement, values in data.items():
|
||||||
popup=folium.features.GeoJsonPopup(
|
popup=folium.features.GeoJsonPopup(
|
||||||
fields=['percent_exprimes'],
|
fields=['percent_exprimes'],
|
||||||
aliases=['Pourcentage:'],
|
aliases=['Pourcentage:'],
|
||||||
style="background-color: white; font-size: 1.5rem;",
|
# style="background-color: white;",
|
||||||
localize=True,
|
localize=True,
|
||||||
sticky=False,
|
sticky=False,
|
||||||
labels=True,
|
labels=True,
|
||||||
|
|
Loading…
Reference in New Issue