-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClass13_InClassDemo_Clean.py
59 lines (51 loc) · 1.8 KB
/
Class13_InClassDemo_Clean.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
#!/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(111)
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()
#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.png')