In [24]:
%matplotlib inline
import os
import json
import ast
import tqdm
from matplotlib import pyplot as plt
def Load(i):
categories = ['DEMOCRATIE_ET_CITOYENNETE',
'LA_FISCALITE_ET_LES_DEPENSES_PUBLIQUES',
'LA_TRANSITION_ECOLOGIQUE',
'ORGANISATION_DE_LETAT_ET_DES_SERVICES_PUBLICS']
with open(f"{categories[i]}.json", 'r', encoding='utf8') as file:
l = json.load(file)
return l
In [25]:
null=None
def Format(entry):
if entry is None:
return ''
if entry.startswith('{'):
entry = eval(entry.split('],')[0]+']}')
return entry
def CarriageReturn(text):
i = 0
for j, e in enumerate(text):
i+=1
if i>30 and e == ' ':
text = text[:j]+'\n'+text[j+1:]
i =0
return text
def PrintQuestions(l):
choices = {}
for response in l[2]['responses']:
value = Format(response['value'])
if type(value) == dict:
boolean = 'fermée'
else:
boolean = 'ouverte'
In [26]:
def PlotChoices(l):
choices = {}
for contribution in tqdm.tqdm(l):
for response in contribution['responses']:
try:
value = Format(response['value'])
except Exception as e:
print(e, value)
continue
if type(value) != dict:
continue
title = response["questionTitle"]
if title not in choices:
choices[title] = {}
for choice in value['labels']:
if choice not in choices[title]:
choices[title][choice] = 0
choices[title][choice] += 1
for question, answer in choices.items():
i = 0
x, y = zip(*answer.items())
question+=f' ({sum(y)} réponses)'
fig, ax = plt.subplots()
for ex, ey in zip(x, y):
bars = plt.bar(ex,ey)
for rect in bars:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width()/2.0, height, f'{int(height/sum(y)*100)}%', ha='center', va='bottom')
plt.title(CarriageReturn(question))
plt.legend([CarriageReturn(text) for text in x],loc='center left', bbox_to_anchor=(1, 0.5))
In [27]:
ls = Load(1)
PlotChoices(ls)
In [28]:
ls = Load(2)
PlotChoices(ls)
In [29]:
ls = Load(3)
PlotChoices(ls)
In [30]:
ls = Load(0)
PlotChoices(ls)
In [ ]:
In [ ]: