-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmypyplot.py
More file actions
48 lines (41 loc) · 1.42 KB
/
Copy pathmypyplot.py
File metadata and controls
48 lines (41 loc) · 1.42 KB
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
# Created By: Theodore Tenedorio
# Date 10/29/2015
# Requires: Python2.7, matplotlib
# The following line is for ipython notebooks only.
#% matplotlib inline
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import MinuteLocator, HourLocator, DayLocator, WeekdayLocator, MonthLocator, YearLocator, DateFormatter
# Dynamically scales a plot to depict data over a time range
def plot_date_range(dates, data, linefmt=['k','b','r'], figsize=(15, 10)):
delta = max(dates) - min(dates)
# Determine date range scale
if abs(delta.days) > 2*365:
major = YearLocator()
minor = MonthLocator()
fmt = DateFormatter('%Y')
elif abs(delta.days) > 60:
major = MonthLocator()
minor = DayLocator()
fmt = DateFormatter('%b \'%y')
elif abs(delta.days) > 0:
major = DayLocator()
minor = HourLocator()
fmt = DateFormatter('%a %b %d')
elif abs(delta.seconds) > 0:
major = HourLocator()
minor = MinuteLocator()
fmt = DateFormatter('%I%p')
else:
print 'ERROR: Expected date range > 0'
return
fig, ax = plt.subplots(figsize=figsize)
for x in range(len(data)):
ax.plot_date(dates, data[x], linefmt[x])
ax.xaxis.set_major_locator(major)
ax.xaxis.set_major_formatter(fmt)
ax.xaxis.set_minor_locator(minor)
ax.autoscale_view()
ax.grid(True)
fig.autofmt_xdate()
plt.show()