forked from goodalljl/hydroinformatics_class
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Class13_InClassDemo_Clean_Subplots.py
98 lines (85 loc) · 3.13 KB
/
Class13_InClassDemo_Clean_Subplots.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Simple demo of using PyMySQL and matplotlib to create a series data plot
from data stored in an ODM database.
"""
import pymysql
import matplotlib.pyplot as plt
from matplotlib import dates
from matplotlib import rc
import datetime
#inputs
SiteID = '2'
VariableID = '36'
StartLocalDateTime = "'2008-01-01'"
EndLocalDateTime = "'2008-12-31'"
#connect to database
conn = pymysql.connect(host='localhost', port=3306, user='root', \
passwd='', db='LBRODM_small')
#extract time series from database
sql_statement = 'SELECT LocalDateTime, DataValue FROM DataValues \
WHERE SiteID = ' + SiteID + ' AND VariableID = ' + VariableID + ' AND \
QualityControlLevelID = 1 AND LocalDateTime >= ' + StartLocalDateTime \
+ ' AND LocalDateTime <= ' + EndLocalDateTime + \
' ORDER BY LocalDateTime'
cursor = conn.cursor()
cursor.execute(sql_statement)
rows = cursor.fetchall()
localDateTimes, dataValues = zip(*rows)
#create plot
fig = plt.figure()
ax = fig.add_subplot(211)
ax.plot(localDateTimes, dataValues, color='grey', linestyle='solid', \
markersize=0)
#set plot properties
ax.set_ylabel("Temperature ($^\circ$C)")
ax.set_xlabel("Date/Time")
ax.xaxis.set_minor_locator(dates.MonthLocator())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))
ax.xaxis.set_major_locator(dates.YearLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n%Y'))
ax.grid(True)
ax.set_title('Water temperature at Little Bear River \n at McMurdy Hollow \
near Paradise, Utah') #hard coded for now. Should update when SiteID is updated.
fig.tight_layout()
#inputs
SiteID = '1'
VariableID = '36'
StartLocalDateTime = "'2008-01-01'"
EndLocalDateTime = "'2008-12-31'"
#connect to database
conn = pymysql.connect(host='localhost', port=3306, user='root', \
passwd='', db='LBRODM_small')
#extract time series from database
sql_statement = 'SELECT LocalDateTime, DataValue FROM DataValues \
WHERE SiteID = ' + SiteID + ' AND VariableID = ' + VariableID + ' AND \
QualityControlLevelID = 1 AND LocalDateTime >= ' + StartLocalDateTime \
+ ' AND LocalDateTime <= ' + EndLocalDateTime + \
' ORDER BY LocalDateTime'
cursor = conn.cursor()
cursor.execute(sql_statement)
rows = cursor.fetchall()
localDateTimes, dataValues = zip(*rows)
#create plot
#fig = plt.figure()
ax = fig.add_subplot(212)
ax.plot(localDateTimes, dataValues, color='grey', linestyle='solid', \
markersize=0)
#set plot properties
ax.set_ylabel("Temperature ($^\circ$C)")
ax.set_xlabel("Date/Time")
ax.xaxis.set_minor_locator(dates.MonthLocator())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))
ax.xaxis.set_major_locator(dates.YearLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n%Y'))
ax.grid(True)
ax.set_title('Water temperature at Little Bear River \n at Mendon Road near \
Mendon, Utah') #hard coded for now. Should update when SiteID is updated.
fig.tight_layout()
#set font type and size for plot
font = {'family' : 'sans-serif', #changed from 'normal' to remove warning
'weight' : 'normal',
'size' : 12}
rc('font', **font)
fig.savefig('Class13_InClassDemo_Clean_Subplots.png')