1
+ import calendar
2
+
3
+ import numpy as np
4
+ import pandas as pd
5
+
6
+ import matplotlib .pyplot as plt
7
+ import seaborn as sns
8
+
9
+ from bs4 import BeautifulSoup
10
+
11
+ data_from_web_txt = open ('./Kaggle_Activity_2022.txt' , 'r' ).read ()
12
+
13
+ bsObj = BeautifulSoup (data_from_web_txt , 'html' )
14
+
15
+ fig_df = pd .DataFrame ()
16
+
17
+ n = 0
18
+
19
+ qdict = {
20
+ 'color-empty' : 0 ,
21
+ 'q1' : 1 ,
22
+ 'q2' : 2 ,
23
+ 'q3' : 3 ,
24
+ }
25
+
26
+ for col in bsObj .find_all ("g" , {"class" :"react-calendar-heatmap-week" }):
27
+ x = int (col ['transform' ].replace ('translate(' , '' ).split (',' )[0 ])
28
+
29
+ for row in col .find_all ('rect' ):
30
+ name = row ['class' ][0 ]
31
+ time = pd .to_datetime (row ['data-tip' ][:24 ])
32
+ y = int (row ['y' ])
33
+ fig_df = pd .concat (
34
+ [
35
+ fig_df ,
36
+ pd .DataFrame .from_dict (
37
+ {n :{
38
+ 'name' : name ,
39
+ 'time' : time ,
40
+ 'value' : qdict [name ],
41
+ 'x' : x ,
42
+ 'y' : y ,
43
+ 'month' : calendar .month_abbr [time .month ],
44
+ }}
45
+ ).T
46
+ ]
47
+ )
48
+ n += 1
49
+
50
+
51
+ fig , ax = plt .subplots (figsize = (15 ,2 ))
52
+ cbar_ax = fig .add_axes ([0.15 , 0.0 , 0.04 , 0.08 ]) # [left, bottom, width, height]
53
+
54
+ month_idx = fig_df .pivot (columns = 'x' , index = 'y' , values = 'month' ).loc [0 ].drop_duplicates ()
55
+ heatmap_df = fig_df .pivot (columns = 'x' , index = 'y' , values = 'value' ).astype (int )
56
+
57
+ g = sns .heatmap (
58
+ heatmap_df ,
59
+ vmax = 4 ,
60
+ vmin = - 0.5 ,
61
+ cmap = 'Blues' ,
62
+ yticklabels = False ,
63
+ linewidths = 2.5 ,
64
+ cbar_kws = {
65
+ 'ticks' :[],
66
+ 'boundaries' : np .arange (0 ,5 ,1 ),
67
+ 'orientation' :'horizontal' ,
68
+ },
69
+ cbar_ax = cbar_ax ,
70
+ ax = ax
71
+ )
72
+
73
+ cbar_ax .get_xaxis ().set_ticklabels ([]);
74
+
75
+ ax .set_xlabel ('' )
76
+ ax .set_ylabel ('' )
77
+ ax .set_xticks (heatmap_df .columns .get_indexer (month_idx .index [1 :]))
78
+ ax .set_xticklabels (month_idx .values [1 :])
79
+
80
+ ax .xaxis .tick_top ()
81
+ ax .xaxis .set_label_position ('top' )
82
+
83
+ ax .set_title ('Activity @ ' + pd .Timestamp .today ().strftime ('%Y-%m-%d' ))
84
+ ax .tick_params (top = False )
85
+
86
+ fig .savefig ('Kaggle_Activity_' + pd .Timestamp .today ().strftime ('%Y' ) + '.png' , bbox_inches = 'tight' )
87
+ fig .savefig ('Kaggle_Activity_' + pd .Timestamp .today ().strftime ('%Y' ) + '.svg' , bbox_inches = 'tight' )
88
+
89
+ plt .show ()
0 commit comments