-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwcplot.py
More file actions
executable file
·80 lines (69 loc) · 2.53 KB
/
wcplot.py
File metadata and controls
executable file
·80 lines (69 loc) · 2.53 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
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
#!/usr/bin/env python
import datetime
import json
from pylab import *
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.mlab as mlab
import matplotlib.cbook as cbook
from matplotlib.dates import DateFormatter, WeekdayLocator, HourLocator, \
DayLocator, MONDAY
def data_dummy():
return [
{"date": datetime.datetime.now(),
"count": 100}
]
def data_real(path):
d = json.load(open(path, "r"))
dts = {}
for x in d:
dt = datetime.datetime.strptime(x["date"], "%d/%m/%Y")
if isinstance(x["result"], basestring):
val = int(x["result"])
if not dt in dts or val > dts[dt]: dts[dt] = val
return sorted([{"date":d, "count":c} for d, c in dts.iteritems()], key=lambda x:x["date"])
def plot(data):
dates = [x["date"] for x in data]
counts = [x["count"] for x in data]
mindate, maxdate = min(dates), max(dates)
daterange = maxdate - mindate
monthdays = mdates.DayLocator(bymonthday = (1, 15))
monthdays2 = mdates.DayLocator(bymonthday = (1,))
mondays = mdates.WeekdayLocator(byweekday=MONDAY)
alldays = DayLocator() # minor ticks on the days
weekFormatter = DateFormatter('%d/%m/%Y') # Eg, Jan 12
dayFormatter = DateFormatter('%d') # Eg, 12
# load a numpy record array from yahoo csv data with fields date,
# open, close, volume, adj_close from the mpl-data/example directory.
# The record array stores python datetime.date as an object array in
# the date column
# datafile = cbook.get_sample_data('goog.npy')
# r = np.load(datafile).view(np.recarray)
fig = plt.figure()
fig.subplots_adjust(bottom=0.2)
ax = fig.add_subplot(111)
ax.plot(dates, counts)
ax.grid()
ax.semilogy()
# format the ticks
if daterange.days > 200:
ax.xaxis.set_major_locator(monthdays2)
elif daterange.days > 100:
ax.xaxis.set_major_locator(monthdays)
else:
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(weekFormatter)
# datemin = datetime.date(r.date.min().year, 1, 1)
# datemax = datetime.date(r.date.max().year+1, 1, 1)
# ax.set_xlim(datemin, datemax)
ax.xaxis_date()
ax.autoscale_view()
setp( gca().get_xticklabels(), rotation=45, horizontalalignment='right')
ax.set_xlabel("Date")
ax.set_ylabel("Word Count")
fig.savefig("out.pdf")
if __name__ == "__main__":
# plot(data_dummy())
plot(data_real("out.json"))