-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphics.py
60 lines (51 loc) · 2.1 KB
/
graphics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import json
import csv
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
import pandas as pd
keys = ["red_dead_redemption_2", "monster_hunter_world", "fallout_76", "marvel_spider_man", "god_of_war"]
def handle_json_load(filename):
with open(filename) as handle:
return json.loads(handle.read())
def generate_pie(data_array, path_file):
labels = 'Negatifs', 'Positifs', 'Neutrals'
explode = explode = (0.02, 0.02, 0.02)
color = ['red', 'blue', 'grey']
fig = plt.figure()
plt.pie(data_array, explode = explode, labels = labels, colors = color, autopct='%1.1f%%', shadow=False, startangle=140)
plt.axis('equal')
fig.savefig(path_file, dpi=fig.dpi)
def generate_graph_date(data, path_file):
fig, ax = plt.subplots(figsize=(10, 10))
plt.xticks(rotation=90)
bars = []
for item in data:
df = pd.DataFrame.from_dict(item, orient='index', columns=['nb'])
b = ax.bar(df.index, df['nb'], width=0.5, align='center')
bars.append(b)
plt.legend(bars, keys, loc='lower left')
plt.title("Evolution du taux d'intérêt en fonction d'une periode donnée")
fig.savefig(path_file, dpi=fig.dpi)
def generate_csv_from_json(data, path_file):
fieldnames = ['city', 'number']
with open(path_file, 'w', newline='') as file:
w = csv.excel(file, fieldnames=fieldnames)
w.writerow({'city': fieldnames[0], 'number': fieldnames[1]})
for key, val in data.items():
w.writerow({'city': key, 'number': val})
result_research = handle_json_load('result.json')
dict = {
"evolutions" : [],
"cities" : []
}
for key in keys:
sentiment = result_research[key]['sentiment']
graph_sentiment_filename = key+'/sentiment.png'
generate_pie(sentiment, graph_sentiment_filename)
dict['evolutions'].append(result_research[key]['trends']['evolutions'])
map_city_filename = key+'/cities.csv'
generate_csv_from_json(result_research[key]['trends']['cities'], map_city_filename)
graph_trends_filename = 'trends.png'
generate_graph_date(dict['evolutions'], graph_trends_filename)