Greetings,
I have the following code to generate a heatmap calendar:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np
import datetime
import calendar
def convert(day):
days_of_week = [2, 3, 4, 5, 6, 7, 1]
return days_of_week[day]
months = [calendar.month_name[month+1] for month in range(12)]
x = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
z = np.zeros((6, 7))
YEAR = 2021
fig = make_subplots(rows=4, cols=3, subplot_titles=months)
annotations = []
row_count = 1
col_count = 1
tot_count = 1
for month in range(12):
month = month + 1
print(calendar.month_name[month])
base = datetime.date(YEAR, month, 1)
month_range = calendar.monthrange(YEAR, month)[1]
date_list = [base + datetime.timedelta(days=x) for x in range(month_range)]
print(date_list)
display = []
for i in range(convert(base.weekday()) - 1):
display.append("")
for j in range(len(date_list)):
display.append(date_list[j].day)
for k in range(6 * 7 - month_range - (convert(base.weekday()) - 1)):
display.append("")
text = np.array(display).reshape((6, 7))[::-1]
for n in range(np.shape(text)[0]):
for m in range(np.shape(text)[1]):
annotations.append(go.layout.Annotation(text=text[n, m], x=m+0.35, y=n+0.35,
xref='x{}'.format(month), yref='y{}'.format(month),
showarrow=False,
font=dict(family='Courier New',
size=10,
color='white')))
z[1, 3] = .6
colorscale=[[0.0, 'Gray'],
[.2, 'rgb(255, 255, 153)'],
[.4, 'rgb(153, 255, 204)'],
[.6, 'rgb(179, 217, 255)'],
[.8, 'rgb(240, 179, 255)'],
[1.0, 'rgb(255, 77, 148)']]
heatmap = go.Heatmap(x=x,
z=z,
hoverinfo='text',
text=text,
colorscale=colorscale,
showscale=False,
opacity=1.0,
xgap=1,
ygap=1,
name=calendar.month_name[month],
)
fig.add_trace(heatmap, row=row_count, col=col_count)
col_count += 1
if col_count == 4:
row_count += 1
col_count = 1
fig.update_yaxes(showticklabels=False)
fig.update_layout(title="2021",
annotations=annotations,
height=2000, width=2000,
)
fig.show()
and it appears to work pretty well except for the month of January some of the annotations are missing (I canβt figure out why). Any ideas?