def __generate_figure(self, user, user_name):
# Set up figure
fig = plt.figure(figsize=(8, 6), dpi=150)
fig.suptitle('{}\'s activity'.format(user_name), fontsize=20)
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(212)
# Plot 24 hour participation data, accumulated over all time
t = [x for x in range(24)]
y = [user['average_day_cycle'][x] for x in t]
ax1.plot(t, y)
y = [user['recent_day_cycle'][x] for x in t]
ax1.plot(t, y)
y = [user['weekly_day_cycle'][x] for x in t]
ax1.plot(t, y)
ax1.set_xlim([0, 24])
ax1.grid()
ax1.set_title('Daily Activity')
ax1.set_xlabel('Hour (UTC)')
ax1.set_ylabel('Message Count per Hour')
ax1.legend(['Average', 'Last Day', 'Last Week'])
# Create pie chart of the most active channels
top5 = sorted(user['participation_per_channel'], key=user['participation_per_channel'].get, reverse=True)[:5]
labels = top5
sizes = [user['participation_per_channel'][x] for x in top5]
explode = [0] * len(top5)
explode[0] = 0.1
ax2.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
# Create overall activity
dates, values = zip(*sorted(user['participation_per_day'].items(), key=lambda dv: dv[0]))
dates = [datetime.fromtimestamp(float(x)) for x in dates]
dates = date2num(dates)
if len(values) > 80:
ax3.bar(dates, values, width=1)
else:
ax3.bar(dates, values)
ax3.xaxis_date()
ax3.set_title('Total Activity')
ax3.set_xlim([dates[0], dates[-1]])
ax3.set_ylabel('Message Count per Day')
ax3.grid()
spacing = 2
for label in ax3.xaxis.get_ticklabels()[::spacing]:
label.set_visible(False)
image_file_name = path.join(self.cache_dir, user_name + '.png')
fig.savefig(image_file_name)
return image_file_name
评论列表
文章目录