-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathxkcd.py
86 lines (70 loc) · 2.71 KB
/
xkcd.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
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
from matplotlib import cm
from matplotlib.ticker import FuncFormatter
from matplotlib.backends.backend_agg import FigureCanvasAgg
def plotXKCD(ngramCSVfile):
fin = open(ngramCSVfile, 'r')
ngrams = fin.readline().strip().split(',')[1:]
data_vals = [[] for ngram in ngrams]
years = []
for line in fin:
sp = line.strip().split(',')
years.append(int(sp[0]))
for i, s in enumerate(sp[1:]):
data_vals[i].append(float(s)*100) # Make percentage
fin.close()
# Set up a figure
plt.xkcd(scale=2, randomness=2.75)
fig = plt.Figure()
canvas = FigureCanvasAgg(fig)
num_ngrams = len(ngrams)
# Plot the data
ax = fig.add_subplot(1, 1, 1)
if len(data_vals) >= 1:
for k, data, label in zip(list(range(num_ngrams)), data_vals, ngrams):
if label.startswith('_'): # The legend doesn't like labels that
label = label[1:] # start with an underscore.
if k == 0:
ax.plot(years, data, label=label,
color=cm.jet(1.*k/num_ngrams), lw=2)
else:
ax.plot(years, data, 'white', lw=6)
ax.plot(years, data, label=label,
color=cm.jet(1.*k/num_ngrams), lw=2)
# Create the Humor-Sans font properties object
prop = fm.FontProperties(fname='Humor-Sans.ttf', size=17)
# Define axes min/max/center
xlim, ylim = ax.get_xlim(), ax.get_ylim()
xmid = (xlim[1] - xlim[0])/2 + xlim[0]
# Create the legend and change the font
legend = ax.legend(loc='best', fontsize=9)
for label in legend.get_texts():
label.set_fontproperties(prop)
# Don't show frame around legend
legend.draw_frame(False)
# Do not display top and right axes
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
# Remove unneeded ticks
ax.tick_params(axis='both', direction='out')
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
# Set tick labels text
prop.set_size(11)
for label in ax.get_xticklabels():
label.set_fontproperties(prop)
for label in ax.get_yticklabels():
label.set_fontproperties(prop)
# Add percentage sign to y-axis ticks
ax.yaxis.set_major_formatter(FuncFormatter(lambda y, pos=0: '%s%%' % y))
# Set the ticks on each axes
ax.set_xticks([xlim[0], xmid, xlim[1]])
ax.set_yticks([ylim[1]])
# Change tick thickness
ax.xaxis.set_tick_params(width=1, length=4)
ax.yaxis.set_tick_params(width=1, length=4)
fig.savefig(ngramCSVfile.replace('.csv', '.png'), dpi=190)
if __name__ == '__main__':
import sys
plotXKCD(sys.argv[1])