-
Notifications
You must be signed in to change notification settings - Fork 8
/
make_plots.py
177 lines (155 loc) · 6.02 KB
/
make_plots.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division, print_function
import json
from dateutil import parser
from datetime import datetime
import matplotlib
matplotlib.use("agg")
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import numpy as np
from operator import itemgetter
lato = fm.FontProperties() # fname="fonts/Lato-Regular.ttf")
__all__ = ["make_plots"]
def plot_cites(ax, year1=2015):
"""Plot citation dates histogram."""
citedates = np.loadtxt("citedates.txt")[1:]
hist, bin_edges = np.histogram(citedates, bins=15)
cdf = np.cumsum(hist)
bins = 0.5 * (bin_edges[1:] + bin_edges[:-1])
ax.plot(bins, cdf, ".", color="C1", ms=3)
ax.plot(bins, cdf, "-", color="C1", lw=3, alpha=0.5)
plt.setp(
ax.get_xticklabels(), rotation=30, fontsize=10, fontproperties=lato, alpha=0.75
)
plt.setp(
ax.get_yticklabels(), rotation=30, fontsize=10, fontproperties=lato, alpha=0.75
)
ax.xaxis.set_major_locator(MaxNLocator(integer=True, nbins=4))
for tick in ax.get_xticklabels() + ax.get_yticklabels():
tick.set_fontsize(10)
ax.set_ylabel("citations", fontsize=16)
ax.set_xlabel("year", fontsize=16)
ax.set_xlim(year1, datetime.now().year + datetime.now().month / 12)
def plot_metrics(ax, year1=2015):
with open("metrics.json") as json_file:
metrics = json.load(json_file)
for i, metric in enumerate(["h", "g", "i10"]):
x, y = np.array(sorted(metrics["time series"][metric].items()), dtype=float).T
inds = x >= 2015
x = x[inds]
y = y[inds]
# HACK to add a little more resolution.
# TODO: Re-think this...
fac = 2
xi = np.repeat(x, fac) + np.tile(np.linspace(0, 1, fac, endpoint=False), len(x))
yi = np.interp(xi, x + 0.5, y)
ax.plot(xi, yi, ".", color="C%d" % i, ms=3)
ax.plot(xi, yi, "-", color="C%d" % i, lw=3, alpha=0.5, label=metric)
plt.setp(
ax.get_xticklabels(), rotation=30, fontsize=10, fontproperties=lato, alpha=0.75
)
plt.setp(
ax.get_yticklabels(), rotation=30, fontsize=10, fontproperties=lato, alpha=0.75
)
ax.xaxis.set_major_locator(MaxNLocator(integer=True, nbins=4))
for tick in ax.get_xticklabels() + ax.get_yticklabels():
tick.set_fontsize(10)
ax.legend(loc="upper left", fontsize=8)
ax.set_ylabel("index", fontsize=16)
ax.set_xlabel("year", fontsize=16)
ax.set_xlim(year1, datetime.now().year + datetime.now().month / 12)
def plot_stars(ax, year1=2015):
"""Plot stargazers histogram."""
with open("stars.json") as json_file:
stars = json.load(json_file)
times = []
for star in stars:
times.append(parser.parse(star["starred_at"]))
tzinfo = times[0].tzinfo
years = range(year1, datetime.now().year + 1)
now = datetime(
datetime.now().year, datetime.now().month, datetime.now().day, tzinfo=tzinfo
)
bins = []
counts = []
for year in years:
for month in range(1, 13):
for day in range(32):
try:
this_bin = datetime(year, month, day, tzinfo=tzinfo)
if this_bin > now:
continue
this_counts = 0
for time in times:
if this_bin >= time:
this_counts += 1
bins.append(matplotlib.dates.date2num(this_bin))
counts.append(this_counts)
except ValueError:
pass
inds = np.array(np.linspace(0, len(bins) - 1, 20), dtype=int)
ax.plot_date(np.array(bins)[inds], np.array(counts)[inds], ".", color="C2", ms=3)
ax.plot_date(bins, counts, "-", color="C2", lw=3, alpha=0.5)
plt.setp(
ax.get_xticklabels(), rotation=30, fontsize=10, fontproperties=lato, alpha=0.75
)
plt.setp(
ax.get_yticklabels(), rotation=30, fontsize=10, fontproperties=lato, alpha=0.75
)
years = list(years)[1::2] # + [datetime.now().year + 1]
ax.set_xticks(
matplotlib.dates.date2num(
[datetime(year, 1, 1, tzinfo=tzinfo) for year in years]
)
)
ax.set_xticklabels(years)
for tick in ax.get_xticklabels() + ax.get_yticklabels():
tick.set_fontsize(10)
ax.set_ylabel("github stars", fontsize=16)
ax.set_xlabel("year", fontsize=16)
ax.margins(0.05, None)
def plot_papers(ax, year1=2015):
"""Plot paper dates histogram."""
# Get pub dates
with open("pubs.json", "r") as f:
pubs = json.load(f)
with open("pubs_manual.json", "r") as f:
pubs_manual = json.load(f)
pubs = sorted(pubs + pubs_manual, key=itemgetter("pubdate"), reverse=True)
pubs = [p for p in pubs if p["doctype"] in ["article", "eprint"]]
pubdates = []
for pub in pubs:
date = int(pub["pubdate"][:4]) + int(pub["pubdate"][5:7]) / 12.0
pubdates.append(date)
hist, bin_edges = np.histogram(pubdates, bins=15)
cdf = np.cumsum(hist)
bins = 0.5 * (bin_edges[1:] + bin_edges[:-1])
ax.plot(bins, cdf, ".", color="C0", ms=3)
ax.plot(bins, cdf, "-", color="C0", lw=3, alpha=0.5)
plt.setp(
ax.get_xticklabels(), rotation=30, fontsize=10, fontproperties=lato, alpha=0.75
)
plt.setp(
ax.get_yticklabels(), rotation=30, fontsize=10, fontproperties=lato, alpha=0.75
)
ax.xaxis.set_major_locator(MaxNLocator(integer=True, nbins=4))
for tick in ax.get_xticklabels() + ax.get_yticklabels():
tick.set_fontsize(10)
ax.set_ylabel("publications", fontsize=16)
ax.set_xlabel("year", fontsize=16)
ax.set_xlim(year1, datetime.now().year + datetime.now().month / 12)
def make_plots():
fig, ax = plt.subplots(1, 5, figsize=(16, 2))
fig.subplots_adjust(wspace=0.6)
plot_papers(ax[0])
plot_cites(ax[1])
plot_stars(ax[2])
plot_metrics(ax[3])
for axis in ax[4:]:
axis.axis("off")
fig.savefig("metrics.pdf", bbox_inches="tight")
if __name__ == "__main__":
make_plots()