Created
May 5, 2016 08:44
-
-
Save mhawksey/c6dd723a79d8b5ca646ceb8827f67242 to your computer and use it in GitHub Desktop.
Calendar Heatmap of FutureLearn steps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#generate a calendar heatmap of step visits | |
# http://stackoverflow.com/a/32492179 | |
# Extension of https://github.com/psychemedia/futurelearnStatsSketches/blob/master/notebooks/FutureLearn%20Stats%20Recipes.ipynb | |
import datetime as dt | |
def generate_data(): | |
num = 100 | |
data = np.random.randint(0, 20, num) | |
start = pd.to_datetime(COURSE_START_DATE) | |
dates = [start + dt.timedelta(days=i) for i in range(num)] | |
return dates, data | |
def calendar_array(dates, data): | |
i, j = zip(*[d.isocalendar()[1:] for d in dates]) | |
i = np.array(i) - min(i) | |
j = np.array(j) - 1 | |
ni = max(i) + 1 | |
calendar = np.nan * np.zeros((ni, 7)) | |
calendar[i, j] = data | |
return i, j, calendar | |
def calendar_heatmap(ax, dates, data): | |
i, j, calendar = calendar_array(dates, data) | |
im = ax.imshow(calendar, interpolation='none', cmap=sns.light_palette("purple", reverse=False,as_cmap=True)) | |
label_days(ax, dates, i, j, calendar) | |
label_months(ax, dates, i, j, calendar) | |
ax.figure.colorbar(im) | |
def label_days(ax, dates, i, j, calendar): | |
ni, nj = calendar.shape | |
day_of_month = np.nan * np.zeros((ni, 7)) | |
day_of_month[i, j] = [d.day for d in dates] | |
for (i, j), day in np.ndenumerate(day_of_month): | |
if np.isfinite(day): | |
ax.text(j, i, int(day), ha='center', va='center') | |
ax.set(xticks=np.arange(7), | |
xticklabels=['M', 'T', 'W', 'T', 'F', 'S', 'S']) | |
ax.xaxis.tick_top() | |
def label_months(ax, dates, i, j, calendar): | |
month_labels = np.array(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', | |
'Aug', 'Sep', 'Oct', 'Nov', 'Dec']) | |
months = np.array([d.month for d in dates]) | |
uniq_months = sorted(set(months)) | |
yticks = [i[months == m].mean() for m in uniq_months] | |
labels = [month_labels[m - 1] for m in uniq_months] | |
ax.set(yticks=yticks) | |
ax.set_yticklabels(labels, rotation=90) | |
#plt.show() | |
tmp = date_limiter(steps[steps['first_visited_at'].notnull()], start=COURSE_START_DATE+' 01:10:50', end=COURSE_END_DATE+' 02', index='first_visited_at') | |
tmp=tmp.reset_index().set_index('first_visited_at') | |
tmp=tmp.groupby(pd.TimeGrouper(freq='D')).size().reset_index() | |
tmp.rename(columns={0:'count'},inplace=True) | |
#tmp = tmp.groupby([pd.Grouper(freq='D',key='first_visited_at')]).count() | |
fig, ax = plt.subplots(figsize=(10, 10)) | |
calendar_heatmap(ax, tmp['first_visited_at'], tmp['count']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment