-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_matplotlibRenter.py
65 lines (51 loc) · 1.93 KB
/
plot_matplotlibRenter.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
60
61
62
63
64
65
#!/usr/bin/Python
"""
Plot all married people with being rented ( X : Income segment , Y : age , Z : Number of visits )
"""
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import requests
import json
result = requests.post('http://localhost:9200/_sql',
data='SELECT * FROM demographics group by MARITAL_STATUS_CODE, AGE_DESC').text
result = json.loads(result)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
c = {
'u': 'r', # unknown
'a': 'b', # single
'b': 'g' # Married
}
m = {
'u': 0,
'a': 1,
'b': 2
}
for bucket in result['aggregations']['MARITAL_STATUS_CODE']['buckets']:
marrige = bucket['key']
age_list = []
count_list = []
for sub_bucket in bucket['AGE_DESC']['buckets']:
age = sub_bucket['key']
peoples = requests.post('http://localhost:9200/_sql',
data='SELECT household_key FROM demographics where AGE_DESC="{age}" and MARITAL_STATUS_CODE="{marrige}"'.format(age=age, marrige=marrige)).text
people_list = []
peoples = json.loads(peoples)
for people in peoples['hits']['hits']:
# print 'people'
people_list.append(people['_source']['household_key'])
# print people_list
count = requests.post('http://localhost:9200/_sql',
data='SELECT count(*) FROM transactions where household_key in ("{lsit}")'.format(lsit='","'.join(people_list))).text
count = json.loads(count)['aggregations']['COUNT(*)']['value']
print count
age_list.append(int(age))
count_list.append(int(count))
ax.bar(np.array(age_list), np.array(
count_list), m[marrige], color=c[marrige])
ax.set_xlabel('Age')
ax.set_ylabel('Count')
ax.set_zlabel('Married')
ax.set_title(' Married people with being owner ( X : Income segment , Y : age , Z : Number of visits )')
#plt.show()