Skip to content

Instantly share code, notes, and snippets.

@lucgiffon
Last active April 18, 2017 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucgiffon/60ac2cf557e7cc280de6e71a1341efdf to your computer and use it in GitHub Desktop.
Save lucgiffon/60ac2cf557e7cc280de6e71a1341efdf to your computer and use it in GitHub Desktop.
Evolution du pourcentage de vote en fonction du pourcentage total de vote utile gagné par macron
from collections import OrderedDict
import numpy as np
import matplotlib.pyplot as plt
from pprint import pprint
# data source : https://fr.wikipedia.org/wiki/Liste_de_sondages_sur_l%27%C3%A9lection_pr%C3%A9sidentielle_fran%C3%A7aise_de_2017#Avril_2017
scores_fillon = [
19.5,
18.5,
21,
19,
20,
19,
18.5,
19,
20,
20
]
mean_fillon = np.mean(scores_fillon)
scores_lepen = [
23,
23,
22,
22,
22,
23,
23,
22,
22,
22
]
mean_lepen = np.mean(scores_lepen)
with open("raw.txt") as f:
lines = f.readlines()
sanit_lines = []
for line in lines[1:]:
elm = ''
sanit_line = []
for c in line:
if c != r'%':
elm += c
elif c == r'%':
token = elm.strip(' \t\n')
token = token.replace(",", ".")
token = token.replace("<0.5", "0.4")
sanit_line.append(float(token))
elm = ''
sanit_lines.append(sanit_line)
arr = np.array(sanit_lines)
mean_arr = np.mean(arr, axis=0)
names = ['Nathalie Arthaud ',
'Philippe Poutou',
'Jean-Luc Mélenchon',
'Benoît Hamon',
'Emmanuel Macron']
ordered_mapping_name_mean = OrderedDict(zip(names, mean_arr))
pprint(ordered_mapping_name_mean)
probas_arr = np.empty_like(mean_arr[:-1])
score_total = sum(mean_arr[:-1])
for i in range(len(mean_arr[:-1])):
probas_arr[i] = mean_arr[i] / score_total
print(probas_arr)
int_macorn_score = int(ordered_mapping_name_mean[names[-1]])
results = np.zeros((int_macorn_score, len(names)))
# each line, one set of horizontal points
for i in range(int_macorn_score):
stolen_value_percent = i
results[i, :-1] = (probas_arr * stolen_value_percent) + mean_arr[:-1]
results[i, -1] = ordered_mapping_name_mean[names[-1]] - stolen_value_percent
for i in range(len(names)):
plt.plot(range(int_macorn_score), list(results[:, i]), label=names[i])
plt.plot(range(int_macorn_score), [mean_fillon] * int_macorn_score, label='Fillon')
plt.plot(range(int_macorn_score), [mean_lepen] * int_macorn_score, label='Lepen')
plt.legend()
plt.xlabel("Pourcentage de vote utile pour Macron")
plt.ylabel("Evolution des intentions de votes des candidats (%)")
plt.savefig("result.png")
print(results)
0,5 % 2 % 18,5 % 9 % 23,5 %
<0,5 % 1 % 19 % 8 % 24 %
0,5 % 2 % 20 % 7,5 % 22 %
0 % 1,5 % 19 % 8 % 24,5 %
0,5 % 2 % 19 % 8 % 22,5 %
1 % 1,5 % 20 % 7,5 % 23 %
0,5 % 2 % 19,5 % 8 % 23 %
1 % 2 % 18 % 8 % 22 %
0,5 % 2 % 18 % 8 % 24 %
0,5 % 2 % 18 % 8 % 24 %
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment