Visualization of time series data using calendar heat map

I believe many people will see such a heat map in GitHub, which records the daily contribution of GitHub platform. Time series data sampled in days in the heat map of each calendar year. GitHub's contribution graph shows the number of contributions made by users in the past few years. The color block represents the amount of contribution, as shown below the color code. From this heat map, we can detect the daily contribution pattern.


time series data

Time series data is a series of data collected over time and sorted according to certain rules, such as hourly, daily, monthly or annual data series in the time series. The application of time series includes sensor readings from industrial processes, weather data such as precipitation, rainfall, temperature or agricultural crop growth, medical records of patients over a period of time, etc. Time series analysis reveals hidden patterns, such as trends or seasonality. Here is a very detailed introduction and suggestions 👉 Time series definition, mean, variance, autocovariance and correlation

Calendar heat map

The calendar heat map uses color cells, usually a single primary color tone, and is extended with its lightness, hue and saturation (such as light to dark blue). It displays the relative number of events per day in the calendar view. Each day is arranged by week, grouped by month and year. This allows you to quickly identify daily and weekly patterns.


Visualization is a good way to gain insight into data. When examining time series data, seasonal or cyclical behavior (if involved) must be understood from the data. Create a heat map using the calplot python library. Calplot creates a heat map from Pandas time series data.

pip install calplot
Collecting calplot
  Downloading calplot- (8.2 kB)
Installing collected packages: calplot
Successfully installed calplot-

Basic drawing

import calplot
import numpy as np; np.random.seed(sum(map(ord, 'calplot')))
import pandas as pd
all_days = pd.date_range('1/1/2019', periods=730, freq='D')
days = np.random.choice(all_days, 500)
events = pd.Series(np.random.randn(len(days)), index=days)

Hide boundary

Boundary related parameter: edgecolor refers to the color of the line separating months. Set it to None to hide the boundary.

all_days = pd.date_range('1/1/2019', periods=360, freq='D')
days = np.random.choice(all_days, 500)
events = pd.Series(np.random.randn(len(days)), index=days)
calplot.calplot(events, edgecolor=None, cmap='YlGn')


Change border year style

Parameter yearlabel_kws passed to Matplotlib set_ The keyword parameter of the ylabel call is used to draw the year of each subgraph. This parameter accepts a dictionary and can set color, size and other styles.

                yearlabel_kws={'color': 'black'},


Add text label

Similar to the ordinary heat map, the specific value can be displayed on each small grid. Controlled by the parameters textformat and textmiller, it represents the string format of the grid cell text and the text style of the grid cell when the text is missing data.

calplot.calplot(events, textformat='{:.0f}',
                textfiller='-', cmap='YlGn')


Hide color bar

There will be a color line on the right of the heat map to indicate the color range of each color. If you don't need to display, you can set the parameter colorbar to False.

calplot.calplot(events, colorbar=False, cmap='YlGn')


Set title

The parameter supertitle that controls the title of the heat map accepts a string.

                suptitle='Random data from standard normal distribution', 


Hide edges of heat map

Control the width parameter linewidth of the line that will be separated every day. If you want to hide it, you can directly set it to 0.

calplot.calplot(events, linewidth=0, cmap='YlGn')


For more details, see:

Posted by airdee on Wed, 25 May 2022 04:47:58 +0300