-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataPlotter.py
213 lines (196 loc) · 8.55 KB
/
dataPlotter.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
import matplotlib
#Allow headless use
if os.environ.get('DISPLAY') is None:
print("Let me search for you: ")
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.colors
import matplotlib.image
import matplotlib.gridspec as gridspec
import matplotlib.dates as mdates
import matplotlib.ticker as mticker
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from numpy import mean
from datetime import datetime
def plot_data(data, filename='plot.png',maxups=None,maxcoms=None,maxage=None,show=False, timeline = None):
'''
Where the plotting magic happens.
plot_data works on a data point. It can be used directly after get_data.
After collect_data, plot_collec should be preferred, to correctly handle the max... args.
If max... args are None, plot_data will freely adapt the chart and color.
timeline contains specific data from plot_collec and is required to plot the upper timeline.
Plotting the timeline requires a full knowledge of the data_collec and therefore
doesnt work on a single data.
'''
def format_title(title,post_sub,analysed_sub,limit_title_len):
'''reformat the title if too long and add the sub name if different from
the analysed sub'''
if post_sub != 'r/'+analysed_sub:
f_title = post_sub + ' - ' + title
else:
f_title = title
if len(f_title) > limit_title_len:
f_title = f_title[:limit_title_len-3]+'...'
return f_title
def crop_image(img, imgheight):
'''cut the thumbnail in the middle of the height'''
topcut = round(len(img)/2) - round(imgheight/2)
bottomcut = round(len(img)/2) + round(imgheight/2)
img = img[topcut:bottomcut, :, :]
return img
def make_colormap_age(maxage,ages,cmapname='GnBu'):
'''prepare the colormap'''
cmap = plt.cm.get_cmap(cmapname)
norm = matplotlib.colors.Normalize(vmin=0, vmax=1.05*maxage)
#avoid that vmax matches the max of the color map, otherwise
#it could be white with certain cmaps
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
cmapage = []
for age in ages:
cmapage.append(cmap(norm(age)))
return cmapage, sm
def rm_frames(ax):
'''shortcut to remove all frames of a subplot'''
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
imgheight = 48 #crop images at 50px high (108px width by default)
limit_title_len = 60 #max nbr of characters in displayed title
figsize = (18,10)
ups = data['ups']
coms = data['coms']
thumbs = data['thumbs']
ages = [age/60 for age in data['ages']]
titles = data['titles']
subs = data['subs']
if not maxage:
maxage = max(ages)
maxage = maxage/60
cmapage, sm = make_colormap_age(maxage=maxage,ages=ages)
maxposts = len(ups)
list_yticks = list(range(1,maxposts+1))
#initiate plot
plt.rcdefaults()
matplotlib.rcParams.update({'font.size': 15})
plt.yticks(list_yticks)
fig = plt.figure(figsize = figsize)
gs = gridspec.GridSpec(2, 5, width_ratios=[0.2,1.5,0.3,1.5,0.05], height_ratios=[1,5])
#Grid is 2 rows * 4 columns
#Top row is for the timeline
#Bottom row is for karma bars / thumbnails / comments bars / colormap legend
#top of the plot, where the timeline is plotted
if timeline:
color_ups = '#549ED6'
color_coms = '#33cc33'
ax_tl = plt.subplot(gs[0,1:5])
rm_frames(ax_tl)
ax_tl.spines['bottom'].set_visible(True)
tl_ups = timeline['ups']
tl_coms = timeline['coms']
tl_ages = [age/60 for age in timeline['ages']]
tl_dates = timeline['dates']
curr_date = datetime.strptime(data['timestamp'],"%b %d %Y %H:%M:%S")
idx_curr_date = tl_dates.index(curr_date)
ax_tl.plot(tl_dates[:idx_curr_date+1],tl_ups[:idx_curr_date+1],color=color_ups)
ax_tl.plot(tl_dates[idx_curr_date:],tl_ups[idx_curr_date:],color=color_ups, alpha=0.1)
ax_tl.set_ylabel('mean(Karma)', color=color_ups)
ax_tl.tick_params('y', colors=color_ups)
ax_tl.yaxis.set_major_locator(mticker.LinearLocator(3))
ax_tl.yaxis.grid(color='grey', linestyle='-', linewidth=0.5, alpha=0.5)
ax_tltwin = ax_tl.twinx()
rm_frames(ax_tltwin)
ax_tltwin.plot(tl_dates[:idx_curr_date+1],tl_ages[:idx_curr_date+1],color=color_coms)
ax_tltwin.plot(tl_dates[idx_curr_date:],tl_ages[idx_curr_date:],color=color_coms, alpha=0.1)
ax_tltwin.set_ylabel('mean(ages in hour)', color=color_coms)
ax_tltwin.yaxis.set_major_formatter(mticker.FormatStrFormatter('%.1f'))
ax_tltwin.tick_params('y', colors=color_coms)
ax_tltwin.yaxis.set_major_locator(mticker.LinearLocator(3))
#ax_tltwin.xaxis.set_major_locator(mdates.HourLocator())
ax_tltwin.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
else: #if no timeline, when plot_data is used on a single data point, fill
#the empty space with a title.
#TODO: improve that part to make better use of the space...
ax_tl = plt.subplot(gs[0,1:])
ax_tl.axis('off')
text_title = 'r/' + data['sub'] + ' - ' + data['timestamp']
ax_tl.text(0.4,0.5,text_title,fontsize=16, fontweight='bold')
#left side of the plot, where the karma is plotted
ax_ups = plt.subplot(gs[1,:2])
rm_frames(ax_ups)
if maxups:
ax_ups.set_xlim(0, maxups)
ax_ups.barh(list_yticks,ups, color = cmapage)
ax_ups.invert_xaxis()
ax_ups.invert_yaxis()
ax_ups.set_xlabel('Karma')
ax_ups.xaxis.set_label_position('bottom')
ax_ups.xaxis.tick_bottom()
ax_ups.set_yticks([])
plt.setp(ax_ups.get_yticklabels(), visible=False)
ax_ups.xaxis.grid(color='grey', linestyle='-', linewidth=0.5, alpha=0.5)
for n in range(len(titles)):
title = format_title(titles[n],subs[n],data['sub'],limit_title_len)
title_pos = ax_ups.get_xlim()[0]
ax_ups.text(title_pos,n+1,' '+title, verticalalignment='center')
#center of the plot, for pictures
ax_thumbs = plt.subplot(gs[1,2], sharey=ax_ups)
ax_thumbs.set_yticklabels([])
ax_thumbs.axis('off')
for n in range(len(thumbs)):
arr_img = crop_image(img=matplotlib.image.imread(thumbs[n]),imgheight=imgheight)
imagebox = OffsetImage(arr_img, 0.7)
ab = AnnotationBbox(imagebox, (0.5, n+1), frameon=False)
ax_thumbs.add_artist(ab)
#right side of the plot, where the comments are plotted
ax_coms = plt.subplot(gs[1,3], sharey=ax_ups)
rm_frames(ax_coms)
if maxcoms:
ax_coms.set_xlim(0,maxcoms)
ax_coms.barh(list_yticks,coms, color = cmapage)
ax_coms.set_xlabel('Comments')
plt.setp(ax_coms.get_yticklabels(), visible=False)
ax_coms.xaxis.set_label_position('bottom')
ax_coms.xaxis.tick_bottom()
ax_coms.xaxis.grid(color='grey', linestyle='-', linewidth=0.5, alpha=0.5)
#colormap legend
ax_cbar = plt.subplot(gs[1,4])
cbar = plt.colorbar(sm, cax = ax_cbar)
cbar.set_label('age in hours')
plt.subplots_adjust(wspace=0.05, hspace=0.2)
#Check existence of plots directory
if not os.path.exists("plots/"):
os.makedirs("plots/")
plt.savefig('plots/'+filename, bbox_inches='tight')
if show: plt.show()
plt.close()
return
def plot_collec(data_collec,maxups=None, maxage=None,maxcoms=None):
'''
Prepares the max... args to make sure that the highest value of the axis
and the max color of the color map covers all the data_collec.
Prepares the timeline_data required for plotting the timeline.
Then, launch the loop to plot each data point.
'''
if not maxups:
maxups = max([max(d['ups']) for d in data_collec])
if not maxcoms:
maxcoms = max([max(d['coms']) for d in data_collec])
if not maxage:
maxage = max([max(d['ages']) for d in data_collec])
nbr_zfill = len(str(len(data_collec)))
timeline = {
'ups':[mean(data['ups']) for data in data_collec],
'coms':[mean(data['coms']) for data in data_collec],
'dates':[datetime.strptime(data['timestamp'],"%b %d %Y %H:%M:%S") for data in data_collec],
'ages': [mean(data['ages']) for data in data_collec]
}
n=1
for data in data_collec:
filename = str(n).zfill(nbr_zfill)+'.png'
plot_data(data,filename=filename,maxups=maxups, maxage=maxage,maxcoms=maxcoms,show=False, timeline = timeline)
n+=1
if __name__ == '__main__':
pass