Learn how to use Dash Bio for next-gen sequencing & quality control. 🧬Register for the Oct 27 webinar.

Heat map with a calendar view to track number of events

Hello team
In matplotlib I’m building graphs like this:


Is it possible to build a similar one using plotly ? Need an idea or example to start from.

Thanks in advance,
Maksim

@yepph
Welcome to forum!

Yes you can define such a calendar as an annotated heatmap:

import calendar
import numpy as np
import plotly.figure_factory as ff

#Create synthetic data
z = np.random.randint(0, 16, (12, 31)).astype(object) #needed to asign np.nan to some elems
nr_monthdays =[ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
for k, d in enumerate(nr_monthdays[:7]):
    n= 31-d
    if n:
        z[k][-n:] = np.nan
for k in range(7, 12):
    z[k]=0
xlabels = np.arange(1, 32).tolist()  #must be a list, otherwise error at the test if x:
ylabels  = [name for name in calendar.month_name][1:]
fig = ff.create_annotated_heatmap(z, xlabels, ylabels, annotation_text=z, colorscale='algae')

fig.update_traces(xgap=2, ygap=2, hoverongaps=False)
fig.update_layout(template='none')
fig.update_yaxes(autorange='reversed', 
                 showgrid=False, 
                 title='month')
fig.update_xaxes(  
                 showgrid=False, 
                 showticklabels=True, 
                 side='bottom', #default side is 'top' for annotated heatmap
                 title='day');

To understand why all updates are needed, just display the fig after running the line:
fig = ff.create_annotated_heatmap(z, xlabels, ylabels, annotation_text=z, colorscale='algae')