forked from mishnit/housing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattributes.py
executable file
·125 lines (106 loc) · 2.51 KB
/
attributes.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import random
import MySQLdb
import matplotlib.pyplot as plot
def randomColor():
result = '%x' % random.randint(0, 16777215)
if len(result) < 6:
return '#0'+result
return '#'+result
def createPlot(axes,connection,query):
# Query for the data.
cursor = connection.cursor()
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
# Reformat the data.
keys = []
values = []
keyAverage = 0
valueAverage = 0
for key, value in results:
# Sometimes key comes out as None. Not sure why.
if key and value:
keys.append(key)
values.append(float(value))
keyAverage += key
valueAverage += value
if len(keys) == 0:
return
keyAverage = float(keyAverage)/float(len(keys))
valueAverage = float(valueAverage)/float(len(values))
# Create the new axes.
color = randomColor()
axes.plot(values,keys,'x',color=color)
axes.plot((valueAverage,),(keyAverage,),'o',color=color)
def main():
# Seed random numbers.
random.seed()
# Connect to the database.
connection = MySQLdb.connect (host = 'localhost',
user = 'root',
passwd = '80017001',
db = 'housing')
# Create the figure.
figure = plot.figure()
axes = figure.add_subplot(111)
labels = []
# Add properties that have 3 bedrooms.
label = '3 bedrooms'
labels.append(label)
query = '''
select %i, list
from houses
where list is not null
and bedrooms = 3
''' % len(labels)
createPlot(axes,connection,query)
# Add properties that have 4 bedrooms.
label = '4 bedrooms'
labels.append(label)
query = '''
select %i, list
from houses
where list is not null
and bedrooms = 4
''' % len(labels)
createPlot(axes,connection,query)
# Add properties that have 2 bathrooms.
label = '2 bathrooms'
labels.append(label)
query = '''
select %i, list
from houses
where list is not null
and bathrooms = 2
''' % len(labels)
createPlot(axes,connection,query)
# Add properties that have 3 bathrooms.
label = '3 bathrooms'
labels.append(label)
query = '''
select %i, list
from houses
where list is not null
and bathrooms = 3
''' % len(labels)
createPlot(axes,connection,query)
# Add properties that have 4 bathrooms.
label = '4 bathrooms'
labels.append(label)
query = '''
select %i, list
from houses
where list is not null
and bathrooms = 4
''' % len(labels)
createPlot(axes,connection,query)
# Setup the axes.
axes.set_ylim(0,len(labels)+1)
axes.set_yticks(range(1,len(labels)+1))
axes.set_yticklabels(labels)
# Plot the data.
plot.show()
# Close the connection.
connection.close()
if __name__ == '__main__':
main()