-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaverage.py
executable file
·72 lines (60 loc) · 1.34 KB
/
average.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
import pdb
import MySQLdb
import matplotlib.pyplot as plot
# Run the SQL.
SQL = '''
'''
def createPlot(axes,color,connection,query):
# Query for the data.
cursor = connection.cursor()
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
# Reformat the data.
dates = []
values = []
for date, value in results:
# Sometimes date comes out as None. Not sure why.
if date and value:
dates.append(date)
values.append(float(value))
# Create the new axes.
axes.plot(dates,values,color)
def main():
# 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)
# Add sold properties.
query = '''
select listdate,
list
from houses
where listdate is not null
and list is not null
order by listdate
'''
createPlot(axes,'ro-',connection,query)
# Add listed properties.
query = '''
select date, average from (
select x.listdate date, avg(y.list) average
from houses x, houses y
where x.id>=20 and x.id between y.id and y.id+19
group by x.id
order by x.id
) data
order by date
'''
createPlot(axes,'go-',connection,query)
# Plot the data.
figure.autofmt_xdate()
plot.show()
# Close the connection.
connection.close()
if __name__ == '__main__':
main()